[SOLVED] Some Help with HTTP Post needed (REST API)

Hello experts,

I currently stuck with a REST API Post Request.
I’am using the Wemos D1 mini Board.
I posted this command with a PLC and Postman already.
Hence the command for itself should be fine.

The client.read() response -1. I guess there is something wrong with the connection to the API.
The WiFi connection works.

Here my code:

#include <ESP8266WiFi.h>

const char* ssid = "SSID";
const char* password = "********";

const char* server = "192.18.18.45";

WiFiClient client;

void setup() {
  Serial.begin(9600);
  delay(1000);

  Serial.println("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  //check wi-fi is connected to wi-fi network
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

void loop() 
{
  if (client.connect(server,80))
  { 

String postStr = "{\r\n\"mission_id\": \"039cf15f-af09-11e9-99fd-0800272ce9e4\",\r\n\"priority\": 0\r\n}";

    client.print("POST /api/v2.0.0/mission_queue HTTP/1.1\n");
    client.print("Host: 192.18.18.45\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/json\n");
    client.print("Authorization: Basic ZjFlZmYxMGQzMTUyYzk1ZjZmMDU5NjU3NmU0ODJiYjhlNDQ4MDY0MzNmNGNmOTI5NzkyODM0YjAxNA==\n");
    client.print("cache-control: no-cache\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
  }
  Serial.println(String(client.read()));
  client.stop();

  Serial.println("Waiting…");

  delay(20000);
}

Thanks in advance for any kind of help.

Cheers
Jones7

read returns -1 if the receive buffer is empty. you should wait for the data

Thank you for your reply.
Do I need to endcode the response?
Now I got the response 84?
I only know the responses 1xx - 5xx.
For example a 200 for success or 401 for Bad request.
Also the post request should execute a action on the Server. Unfortunately that isn't working as well.

Hope you can help me again.

Jones7:
Thank you for your reply.
Do I need to endcode the response?
Now I got the response 84?
I only know the responses 1xx - 5xx.
For example a 200 for success or 401 for Bad request.
Also the post request should execute a action on the Server. Unfortunately that isn't working as well.

Hope you can help me again.

how do you read the response? print all received characters (write them to Serial)

Herein that part of code:

Serial.print(“Waiting for response”);

while(client.read() < 0) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println(String(client.read()));

Jones7:
Herein that part of code:

Serial.print(“Waiting for response”);

while(client.read() < 0) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println(String(client.read()));

this will print ascii code of the first character

you need

while (client.connected()) {
while (client.available()) {
Serial.write(client.read());
}
}

do you have any chance to post your HTTP to another resource where you can check what you have sent?

currently it looks like you are sending two empty lines before payload:

one is in your

String postStr = “{\r\n"mission_id”: “039cf15f-af09-11e9-99fd-0800272ce9e4”,\r\n"priority": 0\r\n}";

And then again a second empty line from here
client.print("\n\n"); // ←
client.print(postStr);

try to remove \r\n from your postStr.

HTTP requires lines to end with \r\n

there should be an empty line (\r\n) after HTTP headers before the content

@noiasca the new lines in the json string are irrelevant for HTTP. it is payload

yes I got mislead by the \n\n which should be a \r\n , then the starting \r\n in postStr could work.

however I would use client.print("\r\n\r\n"); so that the request ends with a double newline, and a postStr without heading \r\n

noiasca:
yes I got mislead by the \n\n which should be a \r\n , then the starting \r\n in postStr could work.

however I would use client.print("\r\n\r\n"); so that the request ends with a double newline, and a postStr without trailing \r\n

I use println(), which on Arduino appends \r\n and I save two bytes of flash with every HTTP request line

When I see the mass of client.print we are not talking about flash nor RAM optimization I guess...

Thanks,

that really helped me out.
I got the complete response now.
With this I was able to fix my request.

Finaly I have it running. I´m very happy right now.