2015-09-07 153 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"