-->
Page 1 of 1

ESP 8862, JSON POST Https request, responsecode -1

PostPosted: Thu Jun 10, 2021 9:32 am
by Sagoos
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:
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 allWaiting 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.

Re: ESP 8862, JSON POST Https request, responsecode -1

PostPosted: Sat Jun 12, 2021 1:02 am
by JurajA
see the examples of the HTTPClient library. the API changed and a WiFiClient or WiFiClientSecure instance is now required as first parameter of begin()