Have figured it out now.

I'm using the restful decorator to expose some web services.

In the example given in the book it shows:

@request.restful()
def api():
    response.view = 'generic.json'
    def GET(tablename,id):
        if not tablename=='person': raise HTTP(400)
        return dict(person = db.person(id))
    def POST(tablename,**fields):
        if not tablename=='person': raise HTTP(400)
        return db.person.validate_and_insert(**fields)
    return locals()


Now the subtle error here is that if you name one of your variables "id" it 
will cause errors when the decorator executes this action. It evidently 
invokes the id function somehow. Leading to some very weird problems.

i.e.

def PUT(id, **vars):
   logging.info(vars)  <-- Vars would be None.

The fix was to do this instead:

def PUT(client_id, **vars):
   logging.info(vars)  <--- Vars would be the expected translated JSON.
   
Perhaps the book could be updated to reflect this.

Thanks for your help,
Matt

On Friday, May 24, 2013 3:08:06 PM UTC+12, Massimo Di Pierro wrote:
>
> Please open a ticket about this.
>
> On Thursday, 23 May 2013 19:35:08 UTC-5, Matt wrote:
>>
>> Hi there,
>>
>> I'm using Version 2.4.6-stable+timestamp.2013.05.17.14.52.19, Running on 
>> mac, Python 2.7.2, no external JSON library installed.
>>
>> Sending the following data via PUT, application/json; charset=UTF-8:
>>
>> {"id": 3}
>>
>> results in a JSON parser error occurring at main.py:
>>
>>     if is_json:
>>         try:
>>             logging.info('before') <--- my addition
>>             json_vars = sj.load(body)
>>             logging.info('after') <--- my addition
>>             body.seek(0)
>>         except Exception, e: <--- my addition
>>             logging.exception(e) <--- my addition
>>             # incoherent request bodies can still be parsed "ad-hoc"
>>             json_vars = {}
>>             pass
>>         # update vars and get_vars with what was posted as json
>>         request.get_vars.update(json_vars)
>>         request.vars.update(json_vars)
>>
>> INFO     2013-05-24 00:07:21,100 main.py:337] before
>> INFO     2013-05-24 00:07:21,100 main.py:338] <module 'json' from 
>> '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py'>
>> ERROR    2013-05-24 00:07:21,100 main.py:346] No JSON object could be 
>> decoded
>> Traceback (most recent call last):
>>   File "/Projects/www/app/gluon/main.py", line 340, in parse_get_post_vars
>>     json_vars = sj.load(body)
>>   File 
>> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py",
>>  
>> line 278, in load
>>     **kw)
>>   File 
>> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py",
>>  
>> line 326, in loads
>>     return _default_decoder.decode(s)
>>   File 
>> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py",
>>  
>> line 366, in decode
>>     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
>>   File 
>> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py",
>>  
>> line 384, in raw_decode
>>     raise ValueError("No JSON object could be decoded")
>>
>> Appears to be related to the id() function for some reason, as changing 
>> the attribute name to anything other than "id" works fine. 
>>
>> Help on built-in function id in module __builtin__:
>>
>> id(...)
>>
>>     id(object) -> integer
>>
>>     Return the identity of an object.  This is guaranteed to be unique 
>> among
>>     simultaneously existing objects.  (Hint: it's the object's memory
>>     address.)
>>
>> If I explicitly force it to use contrib.simplejson. i.e. change the top 
>> of main.py
>>
>> try:
>>     import simplejson as sj #external installed library
>> except:
>>     try:
>>         import contrib.simplejson as sj #pure python library <--- Pasted.
>>     except:
>>         import contrib.simplejson as sj #pure python library
>>
>> I get the same result.
>>
>> Any ideas?
>>
>> Thanks in advance,
>> Matt
>>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to