When I sent a json using curl, the request_body is a string like these:
'{user:johnny , email: [email protected]}'.
But the correct format is '{"user":"johnny" , "email":
"[email protected]"}'. This could be a curl's bug.
So, I've tried do this using jquery and worked for me:
$.ajax({ url:'http://localhost:6543/myurl,
type:'post',
data:JSON.stringify(mydata),
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
Em quarta-feira, 30 de janeiro de 2013 18h30min04s UTC-3, Michael Merickel
escreveu:
>
> 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" <[email protected]<javascript:>>
> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.