【问题标题】:Raw POST request with json in body正文中带有 json 的原始 POST 请求
【发布时间】:2015-12-02 21:40:14
【问题描述】:

在使用 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"
}

我已经为我的身体尝试了另外 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}

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" 时,它可以工作,但我无法获得原始请求

【问题讨论】:

  • 您是否尝试在您的 POST 请求中添加 Content-Length 标头?
  • 而且...是的,这就是解决方案(我昨天经过数小时的调试后找到的)谢谢!
  • 欢迎您!如果它解决了您的问题,那么您发布您的答案可能会很有帮助。

标签: json http curl lua http-post


【解决方案1】:

答案是:我忘了在我的 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

{...some JSON here}

【讨论】:

    【解决方案2】:

    您好,我认为这段代码可以解决您的问题:

    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"
    

    【讨论】:

      猜你喜欢
      • 2020-01-02
      • 2016-02-11
      • 2013-05-30
      • 1970-01-01
      • 2015-08-14
      • 2016-09-29
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      相关资源
      最近更新 更多