Basic of python requests library basic

The official document guidelines are more cumbersome, so this article condenses the necessary content.

pip install requests:

pip  install  requests

Import package:

>>> import requests

get or post request:

# Get request without optional parameters
>>> r = requests.get(url='')
# Post request without optional parameters
>>> r ="")

Other requests (pass by directly, rarely used, and not much in this tutorial)

>>> r = requests.put("")
>>> r = requests.delete("")
>>> r = requests.head("")
>>> r = requests.options("")

get\post parameter description:

HTTP request get and post are the most commonly used, URL is a required parameter, commonly used parameters are params, data, JSON, files, timeout, headers, cookies; other basic unused ones are verified, cert, auth, allow_redirects, proxies, Hooks, stream.

get request text and binary data content

The get request is the simplest, and the amount of data sent is relatively small.

Example: Request with multiple parameters, return text data like paper

import requests

# GET request with parameters, timeout request timeout
params = {'key1':'python','key2':'java'}
r = requests.get(url='', params=params, timeout=3)

# Pay attention to the URL address, it has spliced the parameters together
print('URL address:', r.url)
# Response status code, successful return 200, failure 40x or 50x
print('Request status code:', r.status_code)
print('header information:', r.headers)
print('cookie information:', r.cookies)
print('Response data:', r.text)
# If the response is json data, you can use r.json() to automatically convert to dict
print('Response to json data', r.json())

get returns binary data, such as pictures.

from PIL import Image
from io import BytesIO
import requests

# Request to get the picture and save
r = requests.get('######.jpg')
i =
# # View picture
# Save the picture
with open('img.jpg','wb') as fd:
    for chunk in r.iter_content():

Note: PIL cannot be used, please install pip install pillow

post request, upload form, text, file\picture
Post request is more complicated than get, and the requested data has a variety of forms (form-data), text (json\xml, etc.), file stream (picture\file), etc.

For a post request submitted in the form of a form, only the data needs to be passed to the data parameter of the post() method.

For post requests submitted in the form of JSON text, one way is to dump the JSON data and pass it to the data parameter, and the other way is to pass the JSON data directly to the JSON parameter of the post() method.

For a post request submitted by a single file, stream the file to the files parameter of the post() method.

For post requests submitted by multiple files, set the files to a list of tuples, where the tuple structure is (form_field_name, file_info); then pass the data to the files of the post() method.

post form request:

import requests, json

# With parameter form type post request
r ='######', data=data)
print('Response data:', r.text)

post JSON request:

# json data request
url =''
payload = {'some':'data'}
# You can use json.dumps(dict) to compile the code
r =, data=json.dumps(payload))
print('Response data:', r.text)

# You can directly use json parameters to pass json data
r =, json=payload)
print('Response data:', r.text)

in progress…

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top