make HTTP request with header and parse json

Hey,

I am working with an Arduino Mega 2560 and want to make an HTTP request about a CO2 signal. The problem is that the homepage (API Reference) needs an API key (header). I have the key but I have no idea how to use it with the HTTP request. Anyone who knows how to solve it? Afterwards, i want to receive the json data and parse it. Thanks for help!

void APIabfrage() {
HttpClient client;
// Make a HTTP request:
client.get("https://api.co2signal.com/v1/latest?countryCode=DE");
// headers={"auth-token":"xy"}).json;

// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial2.print(c);

//static const char* queries = { "carbonIntensity", "price"};
// StreamJsonReader jsonreader(queries, 2); // 2 queries
// while(char c = read()){
// jsonreader.process_char(c);
//}

}

With shell, you can just pass the correct header with each request

'https://api.co2signal.com/v1/latest?countryCode=DE'
-H "auth-token: myapitoken
(This information from co2signal.com)

Have a look at Arduinojson library to help parse the data.

G

thanks for your answer? but how to implement #header with arduino?

Client.get(https://api.co2signal.com/v1/latest?countryCode=DK%20&auth-token=xxxxxxxxxxxxxxx);
Pasting https://api.co2signal.com/v1/latest?countryCode=DK%20&auth-token=xxxxxxxxxxxxxxxx into a web search shows the information you would get.

I think you should edityour key as anyone could use it.

G

{"status":"ok","countryCode":"DK","data":{"carbonIntensity":168.02442767378002,"exchange":{"DE":-1427,"NO":1060,"SE":0},"fossilFuelPercentage":22.619113750907342,"production":{"coal":456,"geothermal":null,"oil":158,"solar":222,"unknown":null,"nuclear":null,"gas":367,"biomass":182,"wind":1993,"hydro":null},"storage":{"hydro":null}},"units":{"carbonIntensity":"gCO2eq/kWh","exchange":"MW","price":"EUR/MWh","production":"MW","storage":"MW"}}

G

Perfect, it works!!! Thanks a lot!