If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

 

Here's how you create a restful web2py application in under 2 minutes:
- create a wizard application called 'RT'
- add a table named 'entries' and add a field 'entry wiki'
- finish the wizard with all the default values
- register a user 'user' with password 'pass'
now you have an application with 10 random entries.
 
The next two step are required due to a bug that I filed before (http://goo.gl/CPO0G)
- go to the design page of the app and change the table name from 't_entries' to 'entries'
  in db_wizard.py (4 times) and in default.py (1 time)
- check that the application is still working. you notice that the table now is empty since we changed
  the name of the table.
- Add a couple of entries.
 
- now add the RESTful services to the controller as described in http://goo.gl/iITNd ('parse_as_rest') 
  or here: http://goo.gl/ltfa2
 
@request.restful()
def api():
    response.view = 'generic.'+request.extension
    def GET(*args,**vars):
        patterns = 'auto'
        parser = db.parse_as_rest(patterns,args,vars)
        if parser.status == 200:
            return dict(content=parser.response)
        else:
            raise HTTP(parser.status,parser.error)
    def POST(table_name,**vars):
        return db[table_name].validate_and_insert(**vars)
    def PUT(table_name,record_id,**vars):
        return db(db[table_name]._id==record_id).update(**vars)
    def DELETE(table_name,record_id):
        return db(db[table_name]._id==record_id).delete()
    return locals()

 

- In the browser:
  you get all entries with
 
  you get the just the second entry with
 
  you get all auto-generated patterns with
 
That's as far as we get with GET in a browser.
 
To make things more realistic, we add basic user authentication and try some POSTs with curl.
- Add 
    auth.settings.allow_basic_login = True
    @auth.requires_login()
  above @request.restful. Authentification now is mandatory for the REST interface.
 
- get the first entry
 
- post a new entry
  curl --user user:pass -d "f_entry=something" http://127.0.0.1:8000/RT/default/api/entries.json
 
- delete the first entry
  curl -X DELETE --user user:pass http://127.0.0.1:8000/RT/default/api/entries/1.json
 
 
Same thing from python:
import requests
from requests.auth import HTTPBasicAuth 
payload = {'f_entry': 'somevalue'}
auth=HTTPBasicAuth('user', 'pass')
r = requests.post("http://127.0.0.1:8000/RT/default/api/entries.json", data=payload, auth=auth)
r = requests.delete("http://127.0.0.1:8000/RT/default/api/entries/1.json", data=payload, auth=auth)

 

 

Related slices

Comments (1)

  • Login to post



  • 0
    dirkk0 8 years ago

     

     
    To PUT (=update) something, you need to call:
     
    curl -i -H "Accept: application/json" -X PUT --user user:pass -d "f_entry=something newest" http://127.0.0.1:8000/RT/default/api/entries/1.json
     
    with id=1 being an existing entry.

Hosting graciously provided by:
Python Anywhere