Re: Problems with request.json_body

433 views
Skip to first unread message

Michael Merickel

unread,
Jan 30, 2013, 4:30:04 PM1/30/13
to Pylons

If you manually parse the json yourself using json.loads(request.body) what is the result? This is really all json_body does.

On Jan 30, 2013 8:15 AM, "Rafael Reuber" <rafael...@gmail.com> wrote:
Hey guys,

I'm just trying to send a simple json to my callable view using this beneath. But when the method execution reach request.json_body I alway got an ValueError.
But it's funny because if I'd change request.json_body to request.body, que program prints the json string correctly. So, this is a bug?

Command :curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"user":{"first_name":"firstname","last_name":"lastname"}}' http://localhost:6543/hello

This is my callable view:
@view_config(name='hello',renderer='string')
def hello(request):
print request.json_body
return 'OK' 

Erro message:
Traceback (most recent call last):
  File "D:\Users\u30941.MATRIZ\workspace\myenv\lib\site-packages\pyramid_debugtoolbar-1.0.4-py2.7.egg\pyramid_debugtoolbar\toolbar.py", line 122, in to
olbar_tween
    response = _handler(request)
  File "D:\Users\u30941.MATRIZ\workspace\myenv\lib\site-packages\pyramid_debugtoolbar-1.0.4-py2.7.egg\pyramid_debugtoolbar\panels\performance.py", line
 69, in noresource_timer_handler
    result = handler(request)
  File "D:\Users\u30941.MATRIZ\workspace\myenv\lib\site-packages\pyramid-1.4-py2.7.egg\pyramid\tweens.py", line 21, in excview_tween
    response = handler(request)
  File "D:\Users\u30941.MATRIZ\workspace\myenv\lib\site-packages\pyramid-1.4-py2.7.egg\pyramid\router.py", line 161, in handle_request
    response = view_callable(context, request)
  File "D:\Users\u30941.MATRIZ\workspace\myenv\lib\site-packages\pyramid-1.4-py2.7.egg\pyramid\config\views.py", line 345, in rendered_view
    result = view(context, request)
  File "D:\Users\u30941.MATRIZ\workspace\myenv\lib\site-packages\pyramid-1.4-py2.7.egg\pyramid\config\views.py", line 491, in _requestonly_view
    response = view(request)
  File "d:\users\u30941.matriz\workspace\myenv\myproject\myproject\views.py", line 12, in hello
    print request.json_body
  File "D:\Users\u30941.MATRIZ\workspace\myenv\lib\site-packages\pyramid-1.4-py2.7.egg\pyramid\request.py", line 385, in json_body
    return json.loads(text_(self.body, self.charset))
  File "C:\Python27\Lib\json\__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\Lib\json\decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\Lib\json\decoder.py", line 384, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded



--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rafael Reuber

unread,
Feb 4, 2013, 10:36:35 PM2/4/13
to pylons-...@googlegroups.com
When I sent a json using curl, the request_body is a string like these: '{user:johnny , email: joh...@foolish.com}'. 
But the correct format is '{"user":"johnny" , "email": "joh...@foolish.com"}'. This could be a curl's bug.

So, I've tried do this using jquery and worked for me:

        type:'post',
        data:JSON.stringify(mydata),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json'
});

Marius Gedminas

unread,
Feb 5, 2013, 6:24:59 AM2/5/13
to pylons-...@googlegroups.com
On Mon, Feb 04, 2013 at 07:36:35PM -0800, Rafael Reuber wrote:
> When I sent a json using curl, the request_body is a string like these:
> '{user:johnny , email: joh...@foolish.com}'.
> But the correct format is '{"user":"johnny" , "email":
> "joh...@foolish.com"}'. This could be a curl's bug.

It's more likely a shell quoting bug:

$ echo {"hello": "world"}
{hello: world}

$ echo '{"hello": "world"}'
{"hello": "world"}

Marius Gedminas
--
ultimate_answer_t deep_thought(void) { sleep(years2secs(7500000)); return 42; }
-- ConceptJunkie on Slashdot
signature.asc
Reply all
Reply to author
Forward
0 new messages