Open
Description
I'm trying to authenticate with my API and send a POST.
void saveWeatherData(WEATHER_DATA data) {
String jsonData = weatherDataToJson(data);
Serial.println("Data has been collected");
Serial.println(jsonData);
Serial.println("Saving data...");
HttpClient client = HttpClient(ethernetClient, SERVER_IP, SERVER_PORT);
String urlAuth = generateAuthenticationUrl("root", "root", "password");
client.beginRequest();
client.post(urlAuth, "application/json", "");
client.sendHeader("Authorization", "Basic d2VhdGhlcmxvZ2dlcjp3ZWF0aGVybG9nZ2Vy");
client.endRequest();
String token;
int statusAuth = client.responseStatusCode();
if(statusAuth == 200) {
Serial.println("Authentication OK");
String stringResponse = client.responseBody();
DynamicJsonBuffer jsonBuffer;
JsonObject& jsonResponse = jsonBuffer.parseObject(stringResponse);
token = jsonResponse["access_token"].as<String>();
} else {
Serial.write("Error on authentication ");
Serial.println(statusAuth);
}
delay(3000);
String authHeader = "Bearer ";
authHeader.concat(token);
client.beginRequest();
client.post("/", "application/json", jsonData);
client.sendHeader("Authorization", authHeader);
client.endRequest();
int status = client.responseStatusCode();
if(status == 201) {
Serial.println("Data has been saved");
} else {
Serial.write("Error ");
Serial.println(status);
}
client.stop();
delay(5000);
}
Request to authentication enpoint works well, returning the access token. But the second request always return -3 status. If i make only one request to a random url it works, but the second fails. The second request never appears in my API logs.