Doc: added initial version of network tutorial.

This commit is contained in:
Zbigniew Rębacz 2023-10-21 20:45:51 +02:00
parent f4d9ef4389
commit f99a21a5c0
9 changed files with 260 additions and 34 deletions

View file

@ -0,0 +1,13 @@
description "Obtaining data from the internet using HttpRequest\377";
uses
Core,
Core/SSL;
file
outpuit.json,
main.cpp;
mainconfig
"" = "";

View 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";
}
}

View file

@ -0,0 +1,9 @@
[{
name : {
common : Germany,
official : Federal Republic of Germany,
nativeName : {deu : {official : Bundesrepublik Deutschland, common : Deutschland}}
},
capital : [Berlin]
}]