Ok here is my answer: # RESTFUL CRUD @request.restful() def api(): import json response.view = 'generic.json' def GET(tablename,**vars): # GET VALUES if not tablename=='test': raise HTTP(400) if request.vars.search == None: rows= db(db.test).select() test = row2json(tablename,rows) return test else : rows = db(db.test.testname.contains(vars['search'])).select() test = row2json(tablename,rows) return test def DELETE(tablename,id): if not tablename=='test': raise HTTP(400) return db(db.test.id == id).delete() def PUT(tablename,record_id,**vars): if not tablename=='test': raise HTTP(400) if record_id=='': raise HTTP(400) return db(db.test._id==record_id).update(**vars) def POST(tablename,**vars): if not tablename=='test': raise HTTP(400) return db.test.validate_and_insert(**vars) return locals()
def row2json (tablename,rows): import json rows = rows.as_list() # concat = '{ "Result": "OK", "'+tablename+'": [' concat = '{ "Result": "OK", "Records": [' # concat = '{ "'+tablename+'": [' for row in rows: concat = concat + json.dumps(row)+"," concat = concat.strip(',') concat = concat + ']}' return concat Mike Le samedi 13 février 2016 14:59:53 UTC+1, Mamisoa Andriantafika a écrit : > > Hi, > > I'm trying to ajust the output of the REST services. > > Here is my code: > > @request.restful() > def api(): > response.view = 'generic.json' > def GET(tablename,**vars): # GET VALUES > if not tablename=='test': raise HTTP(400) > if request.vars.search == None: > return dict(test= db(db.test).select()) > else : > return dict(test = db(db.test.testname.contains(vars['search' > ])).select()) > def DELETE(tablename,id): > if not tablename=='test': raise HTTP(400) > return db(db.test.id == id).delete() > def PUT(tablename,record_id,**vars): > if not tablename=='test': raise HTTP(400) > if record_id=='': raise HTTP(400) > return db(db.test._id==record_id).update(**vars) > def POST(tablename,**vars): > if not tablename=='test': raise HTTP(400) > return db.test.validate_and_insert(**vars) > return locals() > > For example, the GET method gives me this output: > {"test": [{"testname": "test18", "id": 4, "testvalue": "119"}, {"testname" > : "test18", "id": 10, "testvalue": "119"}, {"testname": "test19", "id": 11 > , "testvalue": "edt125"}, {"testname": "lmklm", "id": 19, "testvalue": > "opopo"}, {"testname": "test19", "id": 20, "testvalue": "edt125"}, { > "testname": "lmkhjkhj", "id": 21, "testvalue": "lmmmjkhjk"}, {"testname": > "jj", "id": 22, "testvalue": "lmmmjkhjkghgh"}, {"testname": "jjhhh", "id": > 23, "testvalue": "hjhgj"}, {"testname": "jjhhhjhgjh", "id": 24, > "testvalue": "hjhgjhh"}]} > > *How coud I get this output:* > > {*"Result" : "OK"*, "test": [{"testname": "test18", "id": 4, "testvalue": > "119"}, {"testname": "test18", "id": 10, "testvalue": "119"}, {"testname": > "test19", "id": 11, "testvalue": "edt125"}, {"testname": "lmklm", "id": 19 > , "testvalue": "opopo"}, {"testname": "test19", "id": 20, "testvalue": > "edt125"}, {"testname": "lmkhjkhj", "id": 21, "testvalue": "lmmmjkhjk"}, { > "testname": "jj", "id": 22, "testvalue": "lmmmjkhjkghgh"}, {"testname": > "jjhhh", "id": 23, "testvalue": "hjhgj"}, {"testname": "jjhhhjhgjh", "id": > 24, "testvalue": "hjhgjhh"}]} > > Or, another issue,* the output without the "test":* > {"testname": "test18", "id": 4, "testvalue": > ... -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- 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/d/optout.