In this tutorial we’ll learn how to send requests to Igloo using C++, we’ll assume that you are already familiar with the concepts of GraphQL requests and how Igloo works, if not you can check out our Getting Started series.
Sending the first request
To send the raw HTTP requests we will use the cpr library, if you don’t already use it checkout the installation instructions on their documentation.
The key ingredients of a request to Igloo are:
- the payload: it should be a JSON containing a field
querywhose value is the query or mutation to execute in Igloo
- the headers: the request should have an
Content-Typeheader specifying that the payload is a JSON and an
Authorizationheader that contains your access token
- the request verb: requests in GraphQL are always POST requests, even when you are not modifying data
Knowing this we can write a script to fetch our user’s email:
If you substitute your token in the code and run it you should get a response like
When you’ll write your own GraphQL requests be careful to escape any
" characters as
\\\", otherwise the json won’t be valid.
Parsing the response
In general the response of any Igloo request is a JSON containing a field
data if it was successful and a field
errors if something bad happened.
To parse the JSON response we’ll use nlohmann’s json library, to install it check out the instructions on their documentation. Using that library we can extract the email from the response:
To avoid unexpected issues we should check if the response contains any errors before reading the data, to do that you can use the following script: