mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 14:16:07 -06:00
Doc: added initial version of network tutorial.
This commit is contained in:
parent
f4d9ef4389
commit
f99a21a5c0
9 changed files with 260 additions and 34 deletions
13
tutorial/Network01/Network01.upp
Normal file
13
tutorial/Network01/Network01.upp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
description "Obtaining data from the internet using HttpRequest\377";
|
||||
|
||||
uses
|
||||
Core,
|
||||
Core/SSL;
|
||||
|
||||
file
|
||||
outpuit.json,
|
||||
main.cpp;
|
||||
|
||||
mainconfig
|
||||
"" = "";
|
||||
|
||||
43
tutorial/Network01/main.cpp
Normal file
43
tutorial/Network01/main.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <Core/Core.h>
|
||||
|
||||
using namespace Upp;
|
||||
|
||||
CONSOLE_APP_MAIN {
|
||||
HttpRequest http(
|
||||
"https://restcountries.com/v3.1/name/germany?fullText=true&fields=name,capital");
|
||||
auto content = http.Method(HttpRequest::METHOD_GET).Execute();
|
||||
if(content.IsVoid()) {
|
||||
Cout() << "Failed to execute GET request wit error code " << http.GetStatusCode()
|
||||
<< ".\n";
|
||||
return;
|
||||
}
|
||||
|
||||
auto json = ParseJSON(content);
|
||||
if(json.IsError()) {
|
||||
Cout() << "Failed to parse JSON response.";
|
||||
return;
|
||||
}
|
||||
|
||||
if(json.GetCount() == 0) {
|
||||
Cout() << "The JSON is empty. HTTP request returns empty countries list.\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// Let's parse the search results and display them on the terminal.
|
||||
Cout() << "Found countries:\n";
|
||||
for(const auto& result : json) {
|
||||
String common_name;
|
||||
String capital;
|
||||
|
||||
common_name = result["name"]["common"];
|
||||
for(const auto& result_capital : result["capital"]) {
|
||||
capital = result_capital;
|
||||
|
||||
// Some countries like South Africa might have more than one capital city. For this
|
||||
// tutorial, let's ignore it and display only the first result on the list.
|
||||
break;
|
||||
}
|
||||
|
||||
Cout() << "- " << common_name << " with " << capital << " as a capital city.\n";
|
||||
}
|
||||
}
|
||||
9
tutorial/Network01/outpuit.json
Normal file
9
tutorial/Network01/outpuit.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
|
||||
[{
|
||||
name : {
|
||||
common : Germany,
|
||||
official : Federal Republic of Germany,
|
||||
nativeName : {deu : {official : Bundesrepublik Deutschland, common : Deutschland}}
|
||||
},
|
||||
capital : [Berlin]
|
||||
}]
|
||||
Loading…
Add table
Add a link
Reference in a new issue