无法使用ESP8266HttpClient方法读取/获取对POST请求的响应

时间:2020-01-15 13:32:44

标签: c json http arduino esp32

大家早上好, 我目前正在开发基于ESP32的电子纸显示屏的应用程序。我现在想做的是解析一个JSON字符串,其中包含一些针对我的ESP模块的指令。 我有一个服务器在侦听特定的URL。为了从服务器获取JSON,ESP设备必须执行另一个JSON的HTTP POST,该JSON包含与该设备相关的一些信息。因此,基本上,ESP所需的JSON是在ESP本身生成的不同json的POST之后给出的。

我正在使用ESP8266HTTPClient执行此操作,但无法以任何方式从服务器检索JSON。我怀疑我可能做错了与POST标头相关的事情。为了检查发生了什么,我使用Postman执行示例的一些HTTP POST请求,并且它们工作得很好-服务器对其进行验证并发送回所需的JSON。但是到目前为止,我在用ESP执行请求方面还没有成功。

一个效果很好的邮递员请求的代码段示例是:

POST /api/e-ink/v1/post HTTP/1.1
Host: svilmaeink.macomputer.it
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
User-Agent: PostmanRuntime/7.17.1
Accept: */*
Cache-Control: no-cache
Postman-Token: af52ded0-0147-414f-8cea-1519c0094d78,cdb5dd8c-75e7-4053-82dc-56bbaf7e61ef
Host: svilmaeink.macomputer.it
Accept-Encoding: gzip, deflate
Content-Length: 236
Connection: keep-alive
cache-control: no-cache


Content-Disposition: form-data; name="json"

{"wifi_strengh": "68","battery level": "45","device_id": "XX-XX-XX-XX-XX-XM"}
------WebKitFormBoundary7MA4YWxkTrZu0gW--

服务器通过HTTP 200 OK验证此POST请求,并发送以下JSON字符串作为响应:

{
    "reset": false,
    "hw_reset": false,
    "license": false,
    "light_sleep": 60,
    "deep_sleep": 60,
    "date": "2020-01-15 11:43:45",
    "url": "api/image/v1/get?token=vmQCzKeZ14gBAAAABwee1pkIhE6F1HzkG3c-Sg"
}

现在,如果我从ESP发出非常相同的请求,我似乎无法从服务器获得此json字符串。 在下面的代码中,我附上了我在设备上闪存的Arduino代码(使用的库为ESP8266HTTPClient.h):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

String response;

StaticJsonDocument <2000>jsonBuffer;
HTTPClient http;


void getJson()
{
  String payload = ":{\"wifi_strengh\": \"68\",\"battery_level\": \"45\",\"device_id\":\"XX-XX-XX-XX-XX-XM\"}";
  char buffer[sizeof(long)*8+1+4000];
  ltoa(WiFi.RSSI(),buffer,DECIMAL);
  Serial.printf("\nPayload:\n%s", payload.c_str());


  char url[100]="\0";
  strcpy(url, fop.all_vars.HOSTNAME);
  Serial.printf("\n\nQuesto e' l'hostname:\n%s\n", fop.all_vars.HOSTNAME);
  Serial.printf("\nQuesto e' l'url: %s", url);

  http.begin(url);
  http.addHeader("Content-Type", "application/form-data");

  int httpResponseCode = http.POST(payload);

  if(httpResponseCode>0)
  {
    Serial.printf("\nHttp response code is ok ( %d ), trying to retrieve the json now\n", httpResponseCode);
    response = http.getString();
    Serial.printf("%s", response.c_str());
    Serial.println(response);

     // char rsp[response.length()]="\0";
     char *rsp = (char *) malloc((response.length())*sizeof(char));

     strcpy(rsp,response.c_str());
    ma_debug_write(LOGGER_SERIAL_CONSOLE,rsp);
    Serial.printf("\n\nThis is the JSON:\n%s", rsp);

     //parsifichiamo il json ottenuto
     auto error = deserializeJson(jsonBuffer, response.c_str());
     ma_debug_write(LOGGER_SERIAL_CONSOLE,strcat((char *)"Ottenuto il seguente json",response.c_str()));
    if (!error)
    {
      ma_debug_write(LOGGER_SERIAL_CONSOLE,(char *)"deserializeJson() failed with code ");
      ma_debug_write(LOGGER_SERIAL_CONSOLE,(char *)error.c_str());
     }
    else
    {
       //copiamo i dati nella eeprom
      if ( (strcmp(fop.all_vars.DATE,jsonBuffer["date"])==0) )
      {
         fop.all_vars.LICENSE=jsonBuffer["license"];
         fop.all_vars.LIGHT_SLEEP_DELAY=atoi(jsonBuffer["light_sleep"])*1000000000;
         fop.all_vars.DEEP_SLEEP_DELAY=atoi(jsonBuffer["deep_sleep"])*1000000000;
         strcpy(fop.all_vars.DATE,jsonBuffer["date"]);
         strcpy(fop.all_vars.IMG_URL,jsonBuffer["img_url"]);
         ma_debug_write(LOGGER_SERIAL_CONSOLE,(char *)"variabili assegnate adesso le salvo nella eeprom");
         fop.operations.retcode=STATUS_JSON_READ_OK;
         fop.operations.opcode=WKF_WRITE_EEPROM;
         //salvo nella eeprom
         perform_eeprom_op();
       }
     }

     fop.operations.opcode=(jsonBuffer["reset"] || jsonBuffer["hw_reset"])?PROC_HARD_REBOOT:WKF_DSPLY_IMAGE;

  }
 else
  {
     ma_debug_write(LOGGER_SERIAL_CONSOLE,(char *)"Error on sending POST: ");
     Serial.printf("%d", httpResponseCode);
     fop.operations.retcode=STATUS_JSON_READ_FAIL;
     fop.operations.opcode=PWR_LIGHT_SLEEP_MODE;
  }

   http.end();
  }

运行代码时发生的事情是,我将所有内容正确打印在串行监视器上,但是在打印“ Http响应代码正常(204),现在尝试检索json”之后,如果等待1分钟或2分钟,串行监视器添加“ This is the JSON:”(其他是JSON :),除此之外,它出现在Guru Meditation Error(LoadProhibite)中。 我还通过放置一些串行打印来检查发送的字符串是否正常,它们看起来还不错,那里没有问题。尽管如此,我仍无法从服务器检索JSON,ESP执行POST后,HTTPReturnCode仍然为204,而不是200 OK。

我应该怎么做才能做到这一点?

0 个答案:

没有答案
相关问题