summaryrefslogtreecommitdiff
path: root/artima/python/webpy/pizza.py
blob: ae646019d3224a109b6f24c6a60c6831e5bb9c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from urllib import urlencode
from httplib2 import Http
 
def request(url, meth, data='', user=None, passwd=None):
    headers={'content-type' : 'application/x-www-form-urlencoded'}
    h = Http()
    if user:
        # send the header is ('authorization', 'Basic user:pwd') in base64
        # the server should see REMOTE_USER and HTTP_AUTHORIZATION
        h.add_credentials(user, passwd)
    return h.request(url, meth, urlencode(data), headers)
              
if __name__ == '__main__':
    for meth in ("POST", "PUT", "GET", "DELETE"):
        response, content = request(
            'http://localhost:8080/pizza/employee1', meth,
            dict(pizza='margherita', drink='sprite'))
        print response
        print content