具有JSON数据的原始POST请求。

在使用 modeMCU 的 Lua 程序中,我在我的 HTTP POST 请求中遇到问题。

我对 httpbin.org / post 进行测试。

我想发送 json 数据,所以我的请求是:

POST /post HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)
Content-Type: application/json

{...some JSON here}

响应是:

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 07 Sep 2015 10:39:12 GMT
Content-Type: application/json
Content-Length: 332
Connection: close
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)"
  },
  "json": null,
  "origin": "5.51.195.252",
  "url": "http://httpbin.org/post"
}

我尝试了其他两种语法:

POST /post HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)
Content-Type: application/json

json:{...some JSON here}

POST /post HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)
Content-Type: application/json

"json":"{...some JSON here}"

都不行...

你有什么想法吗?

注意:当我使用 curl -v -d @somejson.json -H "Content-Type: application/json" -i -v "http://httpbin.org/post" 时,它可以工作,但我无法获得原始请求。

点赞
用户5211371
用户5211371

答案是:我忘了在 POST 标头中提到“Content-Length”!

Lua 代码:

req = "POST /incomingData/addData/"
        .." HTTP/1.1\r\n"
        .."Host: secret-spire-9368.herokuapp.com\r\n"
        .."Connection: close\r\n"
        .."Accept: */*\r\n"
        .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
        .."Content-Type: application/json\r\n"
        .."Content-Length: "..string.len(json).."\r\n"
        .."\r\n"
        ..json.."\r\n"

正确的 POST 请求:

POST /post HTTP/1.1
Host: httpbin.org
Connection: close
Accept: */*
User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)
Content-Type: application/json
Content-Length: 278

{...在这里添加一些 JSON}
2015-09-12 09:41:37