kernelsauce / turbo

Turbo is a framework built for LuaJIT 2 to simplify the task of building fast and scalable network applications. It uses a event-driven, non-blocking, no thread design to deliver excellent performance and minimal footprint to high-load applications while also providing excellent support for embedded uses.
http://turbo.readthedocs.io/
Apache License 2.0
528 stars 84 forks source link

Request JSON body #225

Closed markuman closed 9 years ago

markuman commented 9 years ago

I'll try to receive some JSON data.

-- http://localhost:8888/api
local api = class("api", turbo.web.RequestHandler)
function api:get()
  self.headers = turbo.httputil.HTTPHeaders()
  self.headers:set_method("POST")
  local g = self:head()
  self:write(g)
end

Trying to send from python results in a 405 error

>>> requests.post('http://localhost:8888/api', json=json.dumps({'Hello': 'Json'}))
<Response [405]>
>>> 

[W 2015/08/25 21:21:57] [web.lua] 405 Method Not Allowed POST /api (127.0.0.1) 1ms

The same happens when trying to send a JSON string from an HTML file (not served by turbo.lua)

<html>
<head>
<script>
function makeRequest()
{
    var xmlhttp = new XMLHttpRequest();   // new HttpRequest instance 
    xmlhttp.open("POST", "http://localhost:8888/api");
    xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
    xmlhttp.send(JSON.stringify({user:"John Rambo", time:"2pm"}));

}
</script>
</head>
<body>
<form name="frm1" id="yourTextBox" onsubmit="makeRequest()">
<input type="submit" value="Submit">
</form>
</body>
</html>

But here turbo.lua results 405 with "options" error.

[W 2015/08/25 21:26:15] [web.lua] 405 Method Not Allowed OPTIONS /api (127.0.0.1) 0ms

I guess CORS is needed and I need to setup some headers but don't know how. Any help?

kernelsauce commented 9 years ago

[W 2015/08/25 21:21:57] [web.lua] 405 Method Not Allowed POST /api (127.0.0.1) 1ms

....

function api:get()

try:

function api:post()

if you want to post from python with

>>> requests.post('http://localhost:8888/api', json=json.dumps({'Hello': 'Json'}))

markuman commented 9 years ago

@kernelsauce Thx, this works. Adding an api:options() functions returns a 200 for XMLHttpRequest too. But unfortunately I've no idea how to access the json data from turbo.lua now. Any hints?

markuman commented 9 years ago

reading the json from python works now, but not from javascript

 -- http://localhost:8888/api
local api = class("api", turbo.web.RequestHandler)
function api:post()
  local json = self:get_json(true)
  for key,value in pairs(json) do print(key,value) end
  print("from python")
end
function api:options()
  local json = self:get_json(true)
  print(json) -- this is nil
  --for key,value in pairs(json) do print(key,value) end
  print("from javascript")
end

The json value is nil (html file from first post). Any ideas what's the difference?

markuman commented 9 years ago

So finally I've figured out how the CORS stuff works and made an example. Maybe this is helpful for others too.

kernelsauce commented 9 years ago

Thank you.