Kitware / tangelo

A simple, quick, powerful web framework
http:/tangelohub.org/tangelo/
Apache License 2.0
185 stars 35 forks source link

Restful server returning an error #489

Closed kaneplusplus closed 9 years ago

kaneplusplus commented 9 years ago
import sys, os

import tangelo
import cherrypy
import requests
import json

@tangelo.restful
def post(*arg, **kwargs):
  request_body = tangelo.request_body().read()
  if len(request_body) == 0:
    print "NO BODY!"
  params = json.loads(request_body)
  text  = params['text']
  return {"text" : text}

Calling the plugin above with:

curl -X POST http://0.0.0.0:8080/saruman -d '{"text": "this will not work"}'

returns the following from tagelo:

SERVICE Could not execute service /test:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tangelo/server.py", line 419, in invoke_service
    result = restfunc(*pargs, **kwargs)
  File "/home/azureuser/entity-extractor/test.py", line 13, in post
    params = json.loads(request_body)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
waxlamp commented 9 years ago

Hi @kaneplusplus!

So I'm not exactly sure why the POST data is not going into the request body, but when I tried this out, I noticed that it is showing up in the query arguments to post() (i.e., in kwargs). You can insert this line at the top of post() to verify on your end:

tangelo.log("SARUMAN", kwargs)

You should see a dict show up in the log with the appropriate text property, when you execute the curl command. If you don't, we will have some more digging to do. If you do, and this doesn't solve your problem (because you particularly depend on data being in the request body, rather than being transferred to the query arguments) then we can look further to see how to accomplish that.

Let me know, and thanks for the bug report!

kaneplusplus commented 9 years ago

Do I need to use the tangelo.request body? It looks like I can just grab the message from kwargs.

[05/Feb/2015:23:15:49] SARUMAN {'{"text": "this will not work"}': u''}
NO BODY!
[05/Feb/2015:23:15:49] SERVICE Could not execute service /test:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tangelo/server.py", line 419, in invoke_service
    result = restfunc(*pargs, **kwargs)
  File "/home/azureuser/entity-extractor/test.py", line 14, in post
    params = json.loads(request_body)
  File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
waxlamp commented 9 years ago

I think you can skip the request_body stuff and just grab the data from kwargs. If this resolves your issue, I will go ahead and close this, though I will make a note to clarify the documentation on this point, and investigate under what circumstances data winds up in the actual body of the request.

kaneplusplus commented 9 years ago

Good enough for me. Thanks for your help.