在ESP8266上发送简单的HTTP发布请求

问题描述 投票:0回答:1

我需要使用ESP8266发送简单的HTTP POST请求。它包含“表单数据”中的数据。

应该看起来像这样:

POST http://testserver.com
{
  "auth_key":"key",
  "data":[
    {
      "key":"temperature",
      "value":31.2
    },
    {
      "key":"humidity",
      "value":50
    }
  ]
}

[对于测试,我使用的是Chrome应用-邮递员。我发送的HTTP请求代码看起来像这样(它是自动生成的):

POST /api/mes HTTP/1.1
Host: testserver.com
Cache-Control: no-cache
Postman-Token: 9b910ed2-afdc-2a11-4963-2f85626cfa4e
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="auth_key"

79bde0ff1efeaee90b1e432c08d324ecfdb532ac42406d7a9a87dd911e95f87e
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="data"

[{"key":"humidity", "value":55}]
------WebKitFormBoundary7MA4YWxkTrZu0gW--

通过邮递员,一切都很好。因此,我通过ESP8266发送了它:

client.setNoDelay(true);
client.println("POST /api/mes HTTP/1.1");
client.println("Host: testserver.com");
client.println("Cache-Control: no-cache");
client.println("Postman-Token: 9b910ed2-afdc-2a11-4963-2f85626cfa4e");
client.println("Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
client.println("");
client.println("------WebKitFormBoundary7MA4YWxkTrZu0gW");
client.println("Content-Disposition: form-data; name=\"auth_key\"");
client.println("");
client.println("79bde0ff1efeaee90b1e432c08d324ecfdb532ac42406d7a9a87dd911e95f87e");
client.println("------WebKitFormBoundary7MA4YWxkTrZu0gW");
client.println("Content-Disposition: form-data; name=\"data\"");
client.println("");
client.println("[{\"key\":\"humidity\", \"value\":55}]");
client.println("------WebKitFormBoundary7MA4YWxkTrZu0gW--");

但是很遗憾,服务器返回的请求不正确。是什么原因引起的?我正在为此而苦苦挣扎,但是我没有其他想法了。

arduino esp8266 arduino-esp8266
1个回答
0
投票

请看这个仓库我已经解决了这个问题

https://github.com/resfandiari/esp8266_upload_file_multipart_form_data

© www.soinside.com 2019 - 2024. All rights reserved.