Request Methods

You can make any type of HTTP request you need. By default Grab will make a GET request.

GET Request

GET is the default request method.

g = Grab()

If you need to pass arguments in through the query string, then you have to build the URL manually:

from urllib import urlencode

g = Grab()
qs = urlencode({'foo': 'bar', 'arg': 'val'})
g.request('' % qs)

If your URL contains unsafe characters then you must escape them manually.

from urllib import quote

g = Grab()
url = u'Россия'

POST Request

To make a POST request you have to specify POST data with the post option. Usually, you will want to use a dictionary:

g = Grab()
g.request('', post={'foo': 'bar'})

You can pass unicode strings and numbers in as values for the post dict, they will be converted to byte strings automatically. If you want to specify a encoding that will be used to convert unicode to byte string, then use request_encoding option.

g = Grab()
g.request('', post={'who': u'конь в пальто'},

If the post option is a string then it is submitted as-is:

g = Grab()
g.request('', post='raw data')

If you want to pass multiple values with the same key use the list of tuples version:

g = Grab()
g.request('', post=[('key', 'val1'), ('key', 'val2')])

By default, Grab will compose a POST request with ‘application/x-www-form-urlencoded` encoding method. To enable multipart/form-data use the post_multipart argument instead of post:

g = Grab()
g.request('', multipart_post=[('key', 'val1'),
                                            ('key', 'val2')])

PUT Request

To make a PUT request use both the post and method arguments:

g = Grab()
g.request('', post='raw data', method='put')

Other Methods

To make DELETE, OPTIONS and other HTTP requests, use the method option.

g = Grab()
g.request('', method='options')