Hey guys Im working on a project that make HTTP calls to a API.
This API returns a JSON. And I was using the Arduino JSON library but I ran into a problem.
I think I cant write over the data in the JSON object.
I have a for loop, and I make x numbers of calls to the API with a different parameter.
The respone I want to save in a JSON object, then copy the result into a typedef struct and then work from there.
I have the following function:
void requestHandler::requestFromApi(String coinId, namePriceTimeStruct *namePriceTimeStruct, uint8_t debug){
if (Serial){
Serial.println("Class requestHandler, function: requestFromApi");
}
String completPath = REQUEST_ADDRESS_API + coinId;
static char buffer[64];
if (Serial && debug){
sprintf(buffer,"Making request to %s", completPath.c_str());
Serial.println(buffer);
}
String payload;
HTTPClient http;
http.begin(clientHttpCall, completPath);
int httpCode = http.GET();
if (httpCode > 0) {
payload = http.getString(); // When I print this, the request is succesfull.
}
http.end();
JsonObject filter_data = filter.createNestedObject("data");
filter_data["id"] = true;
filter_data["price"] = true;
filter_data["price_timestamp"] = true;
DeserializationError error = deserializeJson(doc, payload, DeserializationOption::Filter(filter));
if (error) {
if (Serial){
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
}
} else{
namePriceTimeStruct->idCoin = doc["data"]["id"].as<String>();
namePriceTimeStruct->priceCoin = doc["data"]["price"].as<String>();
String correctDate = doc["data"]["price_timestamp"].as<String>();
correctDate.replace("Z", "");
namePriceTimeStruct->timeStamp = correctDate;
static char buffer[64];
if (Serial && debug){
sprintf(buffer,"data struct %s, %s, %s", namePriceTimeStruct->idCoin, namePriceTimeStruct->priceCoin, namePriceTimeStruct->timeStamp);
Serial.println(buffer);
sprintf(buffer,"data json %s, %s, %s", doc["data"]["id"].as<String>(), doc["data"]["price"].as<String>(), doc["data"]["price_timestamp"].as<String>());
Serial.println(buffer);
}
}
}
This is the Serial.print.. With the first try is works good, then the second one there is nothing in the JSON object. Is there something Im missing with the JSON object / class, or should i do it another way.
Class requestHandler, function: requestFromApi
Making request to http://api.iot.hva-robots.nl/crypto/ADA
data struct ADA, 1.93019129, 2021-11-10T10:51:00
data json ADA, 1.93019129, 2021-11-10T10:51:00
Class requestHandler, function: requestInsertDataCoin
Making call to URL: http://oege.ie.hva.nl:8112/insert/data/ADA/2021-11-10T10:51:00/1.93019129
Class requestHandler, function: requestFromApi
Making request to http://api.iot.hva-robots.nl/crypto/BNB
data struct null, null, null
data json null, null, null
Class requestHandler, function: requestInsertDataCoin
Making call to URL: http://oege.ie.hva.nl:8112/insert/data/null/null/null
Class requestHandler, function: requestFromApi
Making request to http://api.iot.hva-robots.nl/crypto/BTC
data struct null, null, null
data json null, null, null