正文中带有 json 的原始 POST 请求

bix*_*e57 2 lua json curl http http-post

在 Lua 程序中,使用 modeMCU,我的 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}
Run Code Online (Sandbox Code Playgroud)

回应是:

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"
}
Run Code Online (Sandbox Code Playgroud)

我已经为我的身体尝试了 2 种其他语法:

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}
Run Code Online (Sandbox Code Playgroud)

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}"
Run Code Online (Sandbox Code Playgroud)

没有一个工作...

你有想法吗?

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

bix*_*e57 5

答案是:我忘了在我的 POST 标题中提到“内容长度”!

路亚代码:

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"
Run Code Online (Sandbox Code Playgroud)

正确的 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

{...some JSON here}
Run Code Online (Sandbox Code Playgroud)