Raw POST request with json in body

26,644

Solution 1

And the answer is: I forgot to mention the "Content-Length" in my POST headers!

Lua code:

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"

Right POST request:

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}

Solution 2

Hi there, I think this code can solve your problem:

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);

Generated request:

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"}

Received 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"
}

Things to remember

while passing a string to any function or variable which contains double quotes (i.e.""), must be enclosed in single quotes '' like '""')

For Example:

This one will work:

json='{"request":"give me some response"}'

And this will not:

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

here .. is used for concatination of two string:

"POST /post" .. " HTTP/1.1\r\n"

You can also write request as follow:

but here you have to mention the correct Content-Length i.e. the length os json data you sending

 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"
Share:
26,644
bixente57
Author by

bixente57

Updated on July 09, 2022

Comments

  • bixente57
    bixente57 almost 2 years

    In a Lua program, using modeMCU, I am facing a problem with my HTTP POST request.

    I test my request against httpbin.org/post.

    I want to send json data, so my request is :

    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}
    

    The response is :

    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"
    }
    

    I have tried 2 other syntax for my body:

    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}"
    

    None is working ...

    Do you have an idea?

    Note: when I use a curl -v -d @somejson.json -H "Content-Type: application/json" -i -v "http://httpbin.org/post" it works but I cannot get the raw request