JSON POST Request Parse Error, but run good in Postman

Hello,

I'm trying to do a POST request using my Arduino ESP32 Devkit V1 board. It communicates with local server on my network. The server API was using JSON format. When I tried it on Postman, it run good, but when I'm using Arduino with the same format the result was "-32700, Parse error" with status code= 500. Maybe there was a mistake in my JSON formatting? Thank you for your kind help!

Screenshot from Postman

Output from serial:

19:15:01.188 -> making POST request
19:15:02.214 -> Status code: 500
19:15:02.214 -> Response: {"result":null,"error":{"code":-32700,"message":"Parse error"},"id":0}

When I tried (testing) to delete the auth password:

19:24:22.606 -> making POST request
19:24:23.636 -> Status code: 401
19:24:23.636 -> Response: 

My Code:

#include <ArduinoJson.h>
#include <WiFi.h>
#include <ArduinoHttpClient.h>
#include "secrets.h"
const char wifiSsid[] = SECRET_WIFI_SSID;
const char wifiPass[] = SECRET_WIFI_PASS;

char serverAddress[] = "192.168.19.83";  // server address
int port = 2674;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

//String output;
//DynamicJsonDocument doc(1024);
//String postData = "{\"id\":\"\",\"method\":\"getinfo\",\"params\":[]}";
StaticJsonDocument<200> jsonBuffer;
String postData;

void initWiFi()
{
#if defined(ESP8266) || defined(ESP32)
  WiFi.mode(WIFI_STA);
#endif
  WiFi.begin(wifiSsid, wifiPass);
  Serial.print(F("Connecting to WiFi .."));
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println(F("\r\nConnected. IP: "));
  Serial.println(WiFi.localIP());
}

void createJson()
{
  Serial.println("Creating JSON data");
  jsonBuffer.clear(); 
  jsonBuffer["id"] ="";
  jsonBuffer["method"] = "getinfo";
  //jsonBuffer["params"] = [];
  jsonBuffer.createNestedArray("params");
  //serializeJsonPretty(jsonBuffer, Serial);
  serializeJson(jsonBuffer, postData);
  Serial.println(postData);
  delay(1000);
}

void setup() {
  Serial.begin(115200);
  Serial.println("JSON SERIALIZE");
  delay(1000);
  Serial.println("WIFI INIT..");
  initWiFi();
  Serial.println("Creating JSON Data:");
  createJson();
  delay(2000);
}

void loop() {
  Serial.println("making POST request");
  client.beginRequest();
  client.post("/");
  client.sendBasicAuth("telchainuser", "telchainpas");
  client.sendHeader("Content-Type", "application/json");
  client.beginBody();
  client.print(postData);
  client.endRequest();

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();
  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);
  Serial.println("Wait five seconds");
  delay(5000);
}

It looks like the server didn't like your JSON. Does the sever produce any log messages that say what about the parse failed?

HTML Status 500 means "Internal Server Error".

Can you serial print the JSON you are sending?

Ah yes, after I check the debug log and do some experiments here the result:

Result from Arduino:

2022-07-23 13:10:07 mcapi: API request failure: {"method":"","id":null}, code: -32700

Result from Postman(Correct JSON):

2022-07-23 13:11:25 mcapi: API request: {"method":"getinfo","params":[],"id":""}, worker: 27560
2022-07-23 13:11:25 mcapi: API request successful: {"method":"getinfo","id":""}

Result from Postman (try to typo the "getinfo" to "geinfo"):

2022-07-23 13:11:50 mcapi: API request failure: {"method":"geinfo","id":""}, code: -32601

From the log above, it seems that the request from arduino is not sending the "getinfo" sentence. What is wrong from my code?

Thank You.

Yes, here are the print result:
image

I think it is in the correct format, but why on the debug log, the "getinfo" sentences is not appeared?

You might try and hardcode the postdate string to be exactly what you used in Postman as a test. I don't think it should matter, but worth a try.

What do you mean by "hardcode postdate string"? Where can I find that? Could you please elaborate?

Rather than using methods from your JSON library to create postData, just assign it directly:

postData ="{\"method\":\"getinfo\",\"params\":[],\"id\":\"\"}";

The order of items in a JSON object is not supposed to matter, but sometimes the person parsing it ignores that.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.