Question about @request.restful

138 views
Skip to first unread message

Franco

unread,
Jul 21, 2013, 1:46:08 PM7/21/13
to web...@googlegroups.com
Greetings everyone,
I've been using request.restful
to provide an restful interface to my angularjs app,
i tried to post data from the angularjs app and it works, it sends the data but in the POST method i've got an empty dictionary, here is my code and some data logs:

default controller
@request.restful()
def api():
    response
.view = 'generic.json'
   
def GET(table_name, id=None):
       
if table_name in db:
            table
= db[table_name]
           
if id == None:
                query
= (table.id > 0)
           
else:
               
try:
                    id
= int(id or 0)
               
except:
                    id
= 0
                query
= (table.id == id)
           
return {table_name : db(query).select()}
   
def POST(table_name, **vars):
       
print(vars)
   
return locals()

angular code used to post data:
$http.post('http://127.0.0.1:8000/angularjs/default/api/person/', item).success(function(data) {
    console
.log('POST: '+ item.first_name +' Result: '+ data);
});

chrome network log:
  1. Request URL:
  2. Request Method:
    POST
  3. Status Code:
    200 OK
  4. Request Headersview source
    1. Accept:
      application/json, text/plain, */*
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      es,en-US;q=0.8,en;q=0.6
    4. Connection:
      keep-alive
    5. Content-Length:
      46
    6. Content-Type:
      application/json;charset=UTF-8
    7. Cookie:
      csrftoken=hMi7rvIHaxAu09tX0H8c2SDqmwwgS7Ra; session_id_admin=127.0.0.1-9ef67e29-1352-4fef-a621-fcc0c3fa9182; session_id_angularjs=127.0.0.1-9453b1e9-444d-4fe4-8110-9d956bfda316
    8. Host:
    9. Origin:
    10. Referer:
    11. User-Agent:
      Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36
  5. Request Payloadview source
    {id:0, first_name:John, last_name:Doe}
    1. first_name"John"
    2. id0
    3. last_name"Doe"
    1. Response Headersview source
      1. Cache-Control:
        no-store, no-cache, must-revalidate, post-check=0, pre-check=0
      2. Connection:
        keep-alive
      3. Content-Length:
        4
      4. Content-Type:
        text/html; charset=utf-8
      5. Date:
        Sun, 21 Jul 2013 17:38:41 GMT
      6. Expires:
        Sun, 21 Jul 2013 17:38:41 GMT
      7. Pragma:
        no-cache
      8. Server:
        Rocket 1.2.6 Python/2.7.5
      9. Set-Cookie:
        session_id_angularjs=127.0.0.1-9453b1e9-444d-4fe4-8110-9d956bfda316; Path=/
      10. X-Powered-By:
        web2py

    I made some tests using the "requests" module and it works, so for instance i think that the problem is within the way that angularjs sends the data.

    Thanks in advance,

    Franco

    Franco

    unread,
    Jul 23, 2013, 10:37:16 PM7/23/13
    to web...@googlegroups.com
    Anybody?

    Massimo Di Pierro

    unread,
    Jul 25, 2013, 9:07:02 AM7/25/13
    to web...@googlegroups.com
    The problem is that angular sends the request variables as json in body and not urlencoded (an expected) So you must replace

    def POST(table_name, **vars):
            
    print(vars)

    with

    def POST(table_name):
            vars = json.loads(request.body.read())
            
    print(vars)

    Niphlod

    unread,
    Jul 25, 2013, 10:26:07 AM7/25/13
    to web...@googlegroups.com
    ehm? recent web2py should parse application/json POSTs just fine !?!

    Franco

    unread,
    Jul 25, 2013, 10:56:46 AM7/25/13
    to web...@googlegroups.com
    thanks, that's right! now it works. Thanks for the hint ;-)

    Franco

    unread,
    Jul 25, 2013, 10:59:30 AM7/25/13
    to web...@googlegroups.com
    currently i'm using this version: 2.4.6-stable+timestamp.2013.04.06.17.37.38
    Reply all
    Reply to author
    Forward
    0 new messages