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

Hi,

 
1. Create an app in web2py admin panel
 
2. In models/db.py define a table named entries
 
Entries = db.define_table("entries", Field("entry", "text")) 

3. Register a new user in http://localhost:8000/myapp/default/user/register

 

4. Go to appadmin http://localhost:8000/myapp/appadmin and Add a couple of entries.

 
5. now add the RESTful services to the default.py 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 dict(GET=GET, POST=POST, PUT=PUT, DELETE=DELETE)

 

 

6. In the browser:
  you get all entries with
 
7. you get the just the second entry with
 
8. you get all auto-generated patterns with
 
That's as far as we get with GET in a browser.
 
9. 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.
 
10. get the first entry
 
11. post a new entry
  curl --user user:pass -d "f_entry=something" http://127.0.0.1:8000/RT/default/api/entries.json
 
12. delete the first entry
  curl -X DELETE --user user:pass http://127.0.0.1:8000/RT/default/api/entries/1.json
 
 
13. 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 (2)

  • Login to post



  • 0
    raj-chinna-11798 10 years ago

    Hi,

     

    I know it's beginner question, but still am struggling to find a answer for it. 

     

    Currently am building a restful service with my web2py app to get log files from my mobile application, so I need to know how to handle file uploading (server & client side [java]) in the web2py restful services. 

     

    Thanks in advance,

     

     


  • 0
    dirkk0 11 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