Table of contents
Send a request
Pass URL parameters
Response content
Text response content
Binary response content
JSON response content
Original response content
Custom request header
Add request content
Send Multipart-Encoded file
Response status code
Response header
Cookie
Redirection and request history
Errors and exceptions
Send a request
Sending network requests using Requests is very simple, and the supported HTTP request types are: GET, POST, PUT, DELETE, HEAD, and OPTIONS.
# Python version 3.6.5
# Import the request module
import requests
# Send a GET request
r = ('/get')
# Send a POST request
# r = ('/post', data = {'key':'value'})
# Send PUT request
# r = ('/put', data = {'key':'value'})
# Send a DELETE request
# r = ('/delete')
# Send a HEAD request
# r = ('/get')
# Send OPTIONS request
# r = ('/get')
Pass URL parameters
Requests can be passed to the URL by using the params option. Params accepts an object of string dictionary type as a parameter. For example, if you want to pass key1=value1 and key2=value2 to/get, then you can use the following code:
params= {'key1': 'value1', 'key2': 'value2'}
r = ('/get',params=params)
print()
Print result:
/get?key1=value1&key2=value2
You can also pass a list as a value:
params= {'key1': 'value1', 'key2': ['value2', 'value3']}
r = ('/get', params=params)
print()
Print result:
/get?key1=value1&key2=value2&key2=value3
Notice:Keys with the value None in the dictionary will not be added to the URL's query string.
Response content
Text response content
We can read the content of the server response in text:
r = ('/some/endpoint')
# Get the response content in string format
print()
After the request is issued, Requests makes an educated guess on the encoding of the response based on the HTTP header. When you access Requests uses its speculative text encoding. You can find out what encoding Requests uses and be able to change it using properties:
# View the current encoded character set
print()
# Set the current encoded character set
='utf-8'
Notice:Requests can also be used with custom encodings when you need them. If you create your own encoding and register with the codecs module, you can easily use this decoder name as the value and then use Requests to handle the encoding for you.
Decoding method | Return type | Decoding principle | Modify the encoding |
str | Make an informed inference based on the HTTP header's encoding of the response | ='gbk' | |
() | bytes | User-specified | ('utf-8') |
Notice:It is recommended to use () to obtain the response content.
Binary response content
For non-text requests, we can also access the request response body in bytes. For example, to create an image with the binary data returned by the request, you can use the following code:
from PIL import Image
from io import BytesIO
from matplotlib import pyplot
import requests
r = ('/img/baidu_jgylogo3.gif')
# Get the response content in binary format
image = (BytesIO())
#Show pictures
(image)
()
Requests will automatically decode the encoded response data for you with gzip and deflate transmission.
You can also use the following code to save the requested binary image locally, but please note that the extension format of the image file must be consistent with the request address.
# coding=utf-8
import requests
# Send a request
response = ('/img/bd_logo1.png')
# Save the picture
with open('','wb') as f:
()
JSON response content
There is a built-in JSON decoder in Requests to process response data in JSON format.
r = ('/some/endpoint')
# Get the response content in JSON format
print(())
If JSON decoding fails, () will throw an exception. For example, the response content is 401 (Unauthorized), and attempting to access () will throw a ValueError: No JSON object could be decoded exception.
It should be noted that a successful call to () does not mean the success of the response. Some servers will include a JSON object in the failed response (such as HTTP 500 error details). This JSON will be returned by decode. To check if the request is successful, use r.raise_for_status() or check if r.status_code is the same as you expect.
Original response content
We are also able to use Request to get the original socket response from the server:
r = ('/img/baidu_jgylogo3.gif', stream=True)
# Get the original response content
print()
print((10))
# It is recommended to use the following pattern to save text streams to files
with open("D:\\res\\pachong\\", 'wb') as f:
for chunk in r.iter_content(1024):
(chunk)
Custom request header
We can add HTTP headers to the request through the headers option in Requests.
cookie = 'SCF=Alooqnwebz90vN0JG1AwEZ...//......//...'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0',
'cookie': cookie
}
r = ('/some/endpoint', headers=headers)
Add request content
If you want to send some data encoded as form when requesting, you can use the following methods:
import requests
import json
# Method 1 Dictionary Data
data = {'key1': 'value1', 'key2': 'value2'}
# Method 2 Tuple data
data = (('key1', 'value1'), ('key1', 'value2'))
# Method Three JSON data
data = {'some': 'data'}
r = ("/post", data=(data))
Send Multipart-Encoded file
It is very easy to upload multipart-Encoded files using Requests:
# Import the request module
import requests
url = '/post'
# explicitly set file name, file MIME type and request header
files = {'file': ('', open('', 'rb'), 'application/x-javascript', {'Expires': '0'})}
# Send string as file
# files = {'file': ('', 'some,data,to,send\nanother,row,to,send\n')}
r = (url, files=files)
print()
Response status code
Requests uses status_code to detect response status codes, raise_for_status() is used to throw exceptions:
r = ('/get')
print(r.status_code)
print(r.status_code == )
bad_r = ('/status/404')
print(bad_r.status_code)
bad_r.raise_for_status()
Response header
We can view the server response header displayed in a Python dictionary:
print()
{
'content-encoding': 'gzip',
'transfer-encoding': 'chunked',
'connection': 'close',
'server': 'nginx/1.0.4',
'x-runtime': '148ms',
'etag': '"e1ca502697e5c9317743dc078f67693f"',
'content-type': 'application/json'
}
print(['Content-Type'])
Cookie
If a response contains some cookies, you can access them quickly:
url = '/some/cookie/setting/url'
r = (url)
print(['example_cookie_name'])
To send your cookies to the server, you can use the cookies parameter:
url = '/cookies'
cookies = dict(cookies_are='working')
r = (url, cookies=cookies)
print()
Print result:
{
"cookies": {
"cookies_are": "working"
}
}
The return object of a cookie is RequestsCookieJar. It behaves similarly to a dictionary, but the interface is more complete and suitable for cross-domain names and cross-paths. You can also pass Cookie Jar to Requests:
jar = ()
('tasty_cookie', 'yum', domain='', path='/cookies')
('gross_cookie', 'blech', domain='', path='/elsewhere')
url = '/cookies'
r = (url, cookies=jar)
print()
Print result:
{
"cookies": {
"tasty_cookie": "yum"
}
}
Redirection and request history
In Request, you can use the history method of the response object to track redirects.
By default, except for HEAD, Requests automatically handles all redirects, and is a list of Response objects that are created to complete the request. This list of objects is sorted by requests from oldest to closest.
For example, Github redirects all HTTP requests to HTTPS:
r = ('')
print()
print(r.status_code)
print()
Print result:
/
200
[<Response [301]>]
If you are using GET, OPTIONS, POST, PUT, PATCH, or DELETE, you can disable redirection processing via the allow_redirects parameter:
r = ('', allow_redirects=False)
print()
print(r.status_code)
print()
Print result:
/
301
[]
If you use HEAD, you can also enable redirection:
r = ('', allow_redirects=True)
print()
print(r.status_code)
print()
Print result:
/
200
[<Response [301]>]
time out
You can tell requests to stop waiting for the response after the number of seconds set with the timeout parameter has passed. Basically all production code should use this parameter. If not used, your program may lose response forever:
r = ('', timeout=100)
Notice:timeout is only valid for the connection process and has nothing to do with the download of the response body. timeout is not a time limit for the entire download response, but an exception will be raised if the server does not respond within the timeout seconds (more precisely, when no bytes of data was received from the underlying socket in the timeout seconds). If no timeout is specified explicitly, requests do not time out.
Errors and exceptions
When encountering network problems (such as DNS query failure, connection rejection, etc.), Requests will throw a ConnectionError exception.
If the HTTP request returns an unsuccessful status code, Response.raise_for_status() will throw an HTTPError exception.
If the request timed out, a Timeout exception is thrown.
If the request exceeds the set maximum redirection number, a TooManyRedirects exception will be thrown.
All exceptions explicitly thrown by Requests are inherited from .
Reference documentation:/zh_CN/latest/user/