在身体中使用json的原始POST请求

时间:2015-09-07 10:50:16

标签: json http curl lua http-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"时它可以工作,但我无法获得原始请求

2 个答案:

答案 0 :(得分:4)

答案是:我忘了提及" Content-Length"在我的POST标题中!

Lua代码:

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}

正确的POST请求:

{{1}}

答案 1 :(得分: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 = ' {“request”:“给我一些回应”} '

这不会:

  

json = " {“request”:“给我一些回应”} "

此处..用于连接两个字符串:

  

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

您还可以按如下方式编写请求:

但是你必须提到正确的 Content-Length ,即你发送的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"