ESP 8862, JSON POST Https request, responsecode -1
Posted: Thu Jun 10, 2021 9:32 am
Hi! I am currently trying to send some values in a JSON body to a database with my ESP 8266.
When I send the body through Postman everything works great. I have tried to follow tutorials online but when I implement my endpoint, api key and body, I get the responsecode "-1".
Board: LOLIN (WEMOS) D1 mini lite
CODE:
COM
The body that is printed here is the exact same as the one I'm sending in Postman.
When I send the body through Postman everything works great. I have tried to follow tutorials online but when I implement my endpoint, api key and body, I get the responsecode "-1".
Board: LOLIN (WEMOS) D1 mini lite
CODE:
Code: Select all
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
void setup() {
Serial.begin(115200); //Serial connection
WiFi.begin("ssid", "pass"); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Waiting for connection");
}
}
void loop() {
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
StaticJsonBuffer<300> JSONbuffer; //Declaring static JSON buffer
JsonObject& JSONencoder = JSONbuffer.createObject();
JSONencoder["article"] = "3 mm";
JSONencoder["amount"] = 23;
char JSONmessageBuffer[300];
JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
Serial.println(JSONmessageBuffer);
HTTPClient http; //Declare object of class HTTPClient
http.begin("URL/endpoint"); //Specify request destination
http.addHeader("Content-Type", "application/json"); //Specify content-type header
http.addHeader("auth-key", "Bearer <API key>"); //Specify authorization header
int httpCode = http.POST(JSONmessageBuffer); //Send the request
String payload = http.getString(); //Get the response payload
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
http.end(); //Close connection
} else {
Serial.println("Error in WiFi connection");
}
delay(300); //Send a request every 30 seconds
}
COM
Code: Select all
Waiting for connection
{
"article": "3 mm",
"amount": 23
}
-1
The body that is printed here is the exact same as the one I'm sending in Postman.