我在HTTPClient库的POST请求中遇到JSON问题

时间:2019-06-03 15:10:15

标签: arduino esp32

我不能正确使用POST()函数或不知道如何使用

当我尝试在POST()函数内发送Json对象并且得到'{“ error”时,我得到此错误消息'没有匹配的函数来调用'HTTPClient :: POST(ArduinoJson :: JsonObject&)' ParseError“,” description“:”在传入的JSON缓冲区中发现错误“}'消息,当我尝试在POST()函数中将Json简单地写为字符串并尝试以这种方式发送时

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> //v5.13.5

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


void setup() {
  Serial.begin(115200);
  delay(4000);
  WiFi.begin(ssid,password);

  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
  Serial.println("Connected to the WiFi network");


}

void loop() {
  if(WiFi.status() == WL_CONNECTED){
    HTTPClient http;

    http.begin("http://172.20.10.13:1026/v2/entities"); //My docker
    http.addHeader("Content-Type", "application/json");

   StaticJsonBuffer<200> jsonBuffer;
   JsonObject& root = jsonBuffer.createObject();
   root["id"] = "urn:ngsi-ld:Sensor:001";
   root["type"] = "motion";
   root["value"] = "No";
   root.printTo(Serial);
    /*int httpResponseCode = http.POST("{\n\t\"id\":\"urn:ngsi-ld:Sensor:001\", \"type\":\"MotionSensor\",\n\t\"value\":\"NO\"\n}"); */
    int httpResponseCode = http.POST(root);
    if(httpResponseCode > 0){
      String response = http.getString();

      Serial.println(httpResponseCode);
      Serial.println(response);
    }
    else{
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }
  else{
    Serial.println("Error in WiFi connection");
  }
  delay(300000);
}

结果应该是向docker发送的正常POST请求并存储了数据,而我可以在VM的终端中使用GET命令读取该数据

1 个答案:

答案 0 :(得分:0)

HTTPClient.POST的参数是Arduino字符串(或C字符串),您不能简单地传递JSON对象。您需要使用prettyPrintTo将json对象转换为标准JSON字符串,然后将其传递给POST函数。

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> //v5.13.5

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

void setup()
{
    Serial.begin(115200);
    delay(4000);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED)
    { //Check for the connection
        delay(1000);
        Serial.println("Connecting to WiFi..");
    }

    Serial.println("Connection established!");
    Serial.print("IP address:\t");
    Serial.println(WiFi.localIP());
    Serial.println("Connected to the WiFi network");
}

void loop()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        HTTPClient http;

        http.begin("http://172.20.10.13:1026/v2/entities"); //My docker
        http.addHeader("Content-Type", "application/json");

        StaticJsonBuffer<200> jsonBuffer;
        JsonObject &root = jsonBuffer.createObject();
        root["id"] = "urn:ngsi-ld:Sensor:001";
        root["type"] = "motion";
        root["value"] = "No";
        root.printTo(Serial);
        /*int httpResponseCode = http.POST("{\n\t\"id\":\"urn:ngsi-ld:Sensor:001\", \"type\":\"MotionSensor\",\n\t\"value\":\"NO\"\n}"); */
        char json_str[100];
        root.prettyPrintTo(json_str, sizeof(json_str));
        int httpResponseCode = http.POST(json_str);
        if (httpResponseCode > 0)
        {
            String response = http.getString();

            Serial.println(httpResponseCode);
            Serial.println(response);
        }
        else
        {
            Serial.print("Error on sending POST: ");
            Serial.println(httpResponseCode);
        }
        http.end();
    }
    else
    {
        Serial.println("Error in WiFi connection");
    }
    delay(300000);
}