2015-09-07 117 views
0

在Lua程序中,使用modeMCU,我遇到了一个HTTP POST请求的问题。使用json的原始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" 
} 

我已经尝试过其他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} 

and

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"它的工作原理,但我不能让原始请求

+0

你有没有尝试在您的POST请求中添加“Content-Length”标题? – mpromonet

+0

而且...是的,这是解决方案(经过几个小时的调试后,我自己昨天发现)谢谢! – bixente57

+0

欢迎您!如果它能解决您的问题,那么您发布答案可能会很好,也很有帮助。 – mpromonet

回答

2

答案是:我忘了提及的“内容长度”在我的帖子标题!

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 

{...some JSON here} 
0

您好,我想这个代码可以解决你的问题:

conn = net.createConnection(net.TCP, 0) 
json='{"request":"give me some response"}' 
req = "POST /post"      
     .." HTTP/1.1\r\n" 
     .."Host: httpbin.org\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" 
conn:on("receive", function(sck, payload) print(payload) end) 
conn:on("connection", function(sck) 
    sck:send(req)end) 
conn:connect(80, "httpbin.org") 
print(req); 

生成的请求:

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: 35 

{"request":"give me some response"} 

收到的响应:

HTTP/1.1 200 OK 
Connection: close 
Server: gunicorn/19.7.1 
Date: Sun, 23 Apr 2017 20:45:58 GMT 
Content-Type: application/json 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Credentials: true 
Content-Length: 465 
Via: 1.1 vegur 

{ 
    "args": {}, 
    "data": "{\"request\":\"give me some response\"}", 
    "files": {}, 
    "form": {}, 
    "headers": { 
    "Accept": "*/*", 
    "Connection": "close", 
    "Content-Length": "35", 
    "Content-Type": "application/json", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)" 
    }, 
    "json": { 
    "request": "give me some response" 
    }, 
    "origin": "47.8.6.52", 
    "url": "http://httpbin.org/post" 
} 

观光记住

,同时使一个字符串,其包含双引号任何函数或变量(即"",必须用单引号'''""'

例如:

这一个将工作:

JSON = ' { “请求”: “给我一些回应”} '

这样就不会:

JSON = " { “请求”: “给我一些回应”} "

这里..用于两个字符串concatination:

“POST/post”..“HTTP/1.1 \ r \ n”

你也可以写请求如下:

但在这里,就不得不提到正确内容长度即长度OS JSON数据在发送

req = "POST /post"      
      .." HTTP/1.1\r\n" 
      .."Host: httpbin.org\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: 35\r\n" 
      .."\r\n" 
      ..'{"request":"give me some response"}'.."\r\n"