Python Interface test requests library (i)

Source: Internet
Author: User

First, the installation of requests library

Requests library as a third-party library that needs to be installed

cmd mode, run PIP install requests

Second, before learning how to send a request, let's take a look at the requests library and see what he can do.

Print Dir (requests)

Third, analog send GET request

1. Send a GET request with no parameters

r = Requests.get (' https://api.github.com/events ')

Returns a response object that contains the contents of the object, through the Dir (r)

2. Send a GET request with parameters, such as a GET request link http://httpbin.org/get?key2=value2&key1=value1

You need to use the params keyword to write the argument to the params parameter, as follows:

Import REQUESTSR = Requests.get (' http://httpbin.org/get ', params={' key1 ': ' value1 ', ' key2 ': ' value2 '}) print R.url

Parameters can also be separated, as follows

Import requestspayload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}r = Requests.get (' Http://httpbin.org/get ', params=payload) Print R.url

The results of the above operations are:

3. Send a GET request with the request header, need to pass the headers parameter

Import requestsurl= ' https://api.github.com/some/endpoint ' headers = {' user-agent ': ' my-app/0.0.1 '}r = Requests.get ( Url,headers=headers)

Iv. analog sending of Post requests

1. When sending a POST request, you need to put the request parameter in the data parameter

url = ' http://httpbin.org/post ' payload = {' Key1 ': ' value1 ', ' key2 ': ' value2 '}r = requests.post (Url,data = payload)

Python Interface test requests library (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.