Created
April 4, 2022 14:32
-
-
Save ZaninAndrea/5ff7153b736d8fda176621ffc62cd375 to your computer and use it in GitHub Desktop.
Connecting to Igloo using c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cpr/cpr.h> | |
#include <nlohmann/json.hpp> | |
nlohmann::json sendRequest(std::string query, std::string token){ | |
// Send the HTTP request | |
cpr::Response r = cpr::Post(cpr::Url{"https://v1.igloo.ooo/graphql"}, | |
cpr::Body{"{\"query\": \"" + query + "\"}"}, | |
cpr::Header{ | |
{"Content-Type", "application/json"}, | |
{"Authorization", "Bearer "+ token} | |
}); | |
// Parse the response body as JSON | |
nlohmann::json parsedResponse = nlohmann::json::parse(r.text); | |
// Check if the server returned any errors | |
if (parsedResponse["errors"] != nullptr){ | |
std::cerr << parsedResponse["errors"][0]["message"].get<std::string>() << std::endl; | |
throw "GraphQL Error"; | |
} | |
return parsedResponse["data"]; | |
} | |
int main(int argc, char** argv) { | |
std::string query = "{ user { email } }"; | |
std::string token = "replace-your-token-here"; | |
nlohmann::json parsedResponse = sendRequest(query, token); | |
std::string email = parsedResponse["user"]["email"].get<std::string>(); | |
std::cout << email << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment