Different types of request contents

Requests has the facility to deal with different types of Request contents like binary response content, JSON response content, and raw response content. To give a clear picture on different types of response content, we listed the details. The examples used here are developed using Python 2.7.x.

Custom headers

We can send custom headers with a request. For that, we just need to create a dictionary with our headers and pass the headers parameter in the get, or post method. In the dictionary, key is the name of the header and the value is, well, the value of the pair. Let us pass an HTTP header to a request:

>>> import json
>>> url = 'https://api.github.com/some/endpoint'
>>>  payload = {'some': 'data'}
>>> headers = {'Content-Type': 'application/json'}
>>> r = requests.post(url, data=json.dumps(payload), headers=headers)

This example has been taken from the Request documents found at http://docs.python-requests.org/en/latest/user/quickstart/#custom-headers.

In this example, we have sent a header content-type with a value application/json, as a parameter to the request.

In the same way, we can send a request with a custom header. Say we have a necessity to send a request with an authorization header with a value as some token. We can create a dictionary with a key 'Authorization' and value as a token which would look like the following:

>>> url = 'some url'
>>>  header = {'Authorization' : 'some token'}
>>> r.request.post(url, headers=headers)

Sending form-encoded data

We can send form-encoded data like an HTML form using Requests. A simple dictionary to the data argument gets this done. The dictionary of data will turn as form-encoded automatically, when a request is made.

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = request.post("some_url/post", data=payload)
>>> print(r.text)
{

   "form": {
       "key2": "value2",
     "key1": "value1"
   },

}

In the preceding example, we tried sending data that is form-encoded. While dealing with data that is not form-encoded, we should send a string in the place of a dictionary.

Posting multipart encoded files

We tend to upload multipart data like images or files through POST. We can achieve this in requests using files which is a dictionary of 'name' and value of file-like-objects. And also we can specify it as 'name', and value could be 'filename', fileobj just like in the following way:

{'name' : file-like-objects} or
{'name': ('filename',  fileobj)}

The example is as follows:

>>> url = 'some api endpoint'
>>> files = {'file': open('plan.csv', 'rb')}
>>> r = requests.post(url, files=files)

We can access the response using 'r.text'.
>>>  r.text
{

   "files": {
       "file": "< some data … >"
       },
   ….
}

In the former example, we didn't specify the content-type or headers. To add to that, we have the capability to set the name for the file we are uploading:

>>> url = 'some url'
>>> files = {'file': ('plan.csv', open('plan.csv', 'rb'), 'application/csv', {'Expires': '0'})}
>>> r = requests.post(url, files)
>>> r.text
{

   "files"
       "file": "< data...>"
       },

}

We can also send strings to be received as files in the following way:

>>> url = 'some url'
>>> files = {'file' : ('plan.csv', 'some, strings, to, send')}
>>> r.text
{

   "files": {
       "file": "some, strings, to, send"
    },

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.176.145