Python Interface Automation Test II (REQUEST.GET)

Source: Internet
Author: User
Tags error handling

After the environment is set up, let's look at some simple uses of requests, including:

    1. Requests commonly used request methods, including: Get,post
    2. Use of session and cookie in requests library
    3. Other advanced sections: Authentication, proxy, certificate validation, timeout configuration, error handling, and more.

This section begins with a look at how to send a GET request in the requests library:

First, look at the method definition:

1, to the official document went to the next requests.get () method definition, as follows:

2, click "Source" in the upper right corner, to see its source code as follows:

See the last line Return,get method is finally implemented by calling the Requests.request method, in fact, in other methods such as Post,put,head,delete request method, such as the invocation method, The type of the requested method is then passed to the request method's first parameter.

3, the HTTP protocol is a request/response mode-based, stateless, application-level protocol. Since there is a request, there is a response, to see the response information commonly used in Resquest:

Second, the Get method is simple to use:

1. Get with no parameters:

#-*-Coding:utf-8-*-#不带参数的getimport requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([Host, Endpoint]) R = requests.get (URL) #response = R.json () print type (r.text) print (eval (r.text))

Output:

{    ' origin ': ' 183.14.133.88 ',    ' headers ': {        ' Connection ': ' Close ', '        Host ': ' httpbin.org ',        ' Accept-encoding ': ' gzip,        deflate ',        ' Accept ': ' */* ',        ' user-agent ': ' python-requests/2.18.1 '    },    ' args ': {            },    ' url ': ' http://httpbin.org/get '}

2. Get with parameters:
#-*-Coding:utf-8-*-#带参数的getimport requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([host,e Ndpoint]) params = {"Show_env": "1"}r = Requests.get (url=url,params=params) Print R.url

Output:

http://httpbin.org/get?show_env=1{    ' origin ': ' 183.14.133.88 ',    ' headers ': {        ' X-request-id ': ' Ebe922b4-c463-4fe9-9faf-49748d682fd7 ',        ' accept-encoding ': ' gzip,        deflate ',        ' x-forwarded-port ': ' 80 ', '        total-route-time ': ' 0 ',        ' Connection ': ' Close ',        ' connect-time ': ' 0 ',        ' Via ': ' 1.1vegur ',        ' X-forwarded-for ': ' 183.14.133.88 ', '        Accept ': ' */* ',        ' user-agent ': ' python-requests/2.18.1 ',        ' X-request-start ': ' 1504755961007 ',        ' Host ': ' httpbin.org ',        ' x-forwarded-proto ': ' http '    },    ' Args ': {        ' show_env ': ' 1 '    },    ' url ': ' http://httpbin.org/get?show_env=1 '}

3. Get with Header:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([Host,endpoint] ) headers = {"User-agent": "Test request Headers"}r = Requests.get (URL) r = Requests.get (url,headers=headers) #response = R.json () Print (eval (r.text)) [' Headers '] [' user-agent ']

Output:

Test request Headers

4, with parameters and header:

#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([Host,endpoint] ) headers = {"User-agent": "Test request Headers"}params = {"Show_env": "1"}r = Requests.get (URL) r = requests.get (URL, Headers=headers,params=params) #response = R.json () print (eval (r.text)) [' Headers '] [' user-agent ']print r.url

Output:

Test Request Headershttp://httpbin.org/get?show_env=1

Python Interface Automation Test II (REQUEST.GET)

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.