As the title says... Chat on...

User avatar
By Vincent Lê
#28342 Hi,

I'm despairingly failing to request a server using POST and a json body.

My syntax:
r
Code: Select alleq = "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"
        .."\r\n"
        ..json.."\"\r\n"             
print(req)


That seems like a good POST http request:
Code: Select allPOST /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

{my json data}


But the response is:
Code: Select allHTTP/1.1 200 OK
Server: nginx
Date: Mon, 07 Sep 2015 11:25:10 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"
}


And should be:
Code: Select allHTTP/1.1 200 OK
Server: nginx
Date: Mon, 07 Sep 2015 11:11:11 GMT
Content-Type: application/json
Content-Length: 858
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Proxy-Connection: Keep-alive

{
  "args": {},
  "data": "{ JSON data }",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "233",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.30.0"
  },
  "json": {JSON data},
  "origin": "5.51.195.252",
  "url": "http://httpbin.org/post"
}

That's what I get when performing a
Code: Select allcurl -d @mysjon.json -H "Content-Type: application/json" -i "http://httpbin.org"


What am I missing ?

Is it possible/recommanded to add the Lua socket.http lib? Maybe it would works better?