Categories
Uncategorized

requests in-depth analysis of the plane and call packages

Foreword

Speaking python HTTP request interface automated testing, my mind flashed the first is probably the requests library, of course, there are a lot of python module can send HTTP requests, including native module http.client, urllib2 and so on, but because of native one module is too complex, cumbersome to use, then requests the library was born, it is also at this stage of the more popular interface automated testing tools

requests a third-party library that encapsulates all HTTP request methods, simple and easy to use, only need to call the corresponding method to complete the whole process of sending network requests according to the different ways of request, so today we are concerned that requests library is how to use it before we started less talk about the main content of today:

How to send a get request 1. requets

2. requests How to send post request

3. params difference parameters and data parameters

Magical 4. requests the Session class library

The request for the get and post requests, requests for a simple package

installation

Usually, before we use third-party libraries, you need to install

cmd Run pip install requests can then be introduced into the module using a method which provides

import requests

Send a get request

requests by calling the get () method to complete the send get request, then, before you get to master how to send requests library request, you should also get a quick look at what is requested, you can refer to this article https: // www. cnblogs.com/linuxchao/p/linuxchao-http.html

Usually before we learn how to use a method, we must first know what the parameters of this method requires? What parameter type is? So we will first analyze the source code get () method

 1 def get(url, params=None, **kwargs):
 2     r"""Sends a GET request.
 3 
 4     :param url: URL for the new :class:`Request` object.
 5     :param params: (optional) Dictionary, list of tuples or bytes to send
 6         in the body of the :class:`Request`.
 7     :param \*\*kwargs: Optional arguments that ``request`` takes.
 8     :return: :class:`Response ` object
 9     :rtype: requests.Response
10     """
11 
12     kwargs.setdefault('allow_redirects', True)
13     return request('get', url, params=params, **kwargs)

This is the source of the get method, you should be able to find, get () method will only pass a parameter url, other non-essential parameters are passed, then the other parameters have any effect?

params

For this parameter can be a dictionary, it can be nested tuples, based on the characteristics of get requests, request parameters can be directly after the URL to? No transfer start key = value form (s parameters using symbols & division), but in order to clearly distinguish the URL and parameters, it is necessary to use transmission parameters params

** kwargs: Some other key parameters, temporarily not describe

Next we look at two simple example, experience the process through reauets get () method sends a get request with no parameters and the parameters of the request

Send request to get access to blog Home Park by get () method

"""
------------------------------------
@Time : 2019/7/11 20:34
@Auth : linux超
@File : requests_blog.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import requests  #

Import module requests

response = requests.get('https://www.cnblogs.com/') #

Send a get request

print(response.text) #

Fetch response data

Response Data


"zh-cn">

    "utf-8" />
    "viewport" content="width=device-width, initial-scale=1" />
    "referrer" content="always" />
    博客园 - 代码改变世界
        "keywords" content="

Developers, blog garden, developers, program ape, procedures Yuan, geek, programming, code, open source, IT website, Developer, Programmer, Coder, Geek, Technology Community

" /> "description" content="

Blog Park is a developer-oriented knowledge-sharing community. Since its inception, has been committed and garden blog for developers focus on creating a pure technical exchange communities to promote and help developers to share knowledge through the Internet, so that more developers to benefit from it. Blog garden's mission is to help developers use the code to change the world.

" /> "shortcut icon" href="//common.cnblogs.com/favicon.ico" type="image/x-icon" /> "Stylesheet" type="text/css" href="/bundles/aggsite.css?v=IlEZk4Ic2eCzcO6r0s4bGm62FAo8VZI-US_0EqUe2Bk1" /> "RSSLink" title="RSS" type="application/rss+xml" rel="alternate" href="http://feed.cnblogs.com/blog/sitehome/rss" />

Here I only interception of a part of the response data, the response data is actually home garden blog HTML source code

You can see only one line of code to complete the whole process the request, the response data obtained by response.text (In fact, this process and we enter the blog address to access the park in the browser is the same), and of course you can also get to use the following method different response data

response.headers  #

Get response headers

response.cookies #

Get cookie returned

response.status_code #

Gets the status code

get sent with a request parameter params

"""
------------------------------------
@Time : 2019/7/11 20:34
@Auth : linux超
@File : requests_blog.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import requests

login_url = r'http://***/futureloan/mvc/api/member/login

' # interface address

login_data = {"mobilephone": "13691579841", "pwd": 123456} # 接口所需参数 response = requests.get(url=login_url, params=login_data) # 发送get请求 print(response.text) # 获取响应数据
print(response.url)

Response Data

{"status":1,"code":"10001","data":null,"msg":"

login successful

"} http://***/futureloan/mvc/api/member/login?mobilephone=13691579841&pwd=123456 Process finished with exit code 0

We give parameter params, with parameters to complete the dictionary by passing a get request, and the fetch response data

note

1. We also preferably daily work as an interface data transfer dictionary, if the data type is json when sending a request needs to turn into a dictionary

2. As long as you send a get request, and interface parameters do not want to pass directly through the url mosaic way, you can only use params parameter to pass (if you use data, json and other parameters you will find that the request does not succeed) because params parameter passed to the url additional features will be after, this is the get request, so you need to keep in mind: params parameter can only be used when sending a request to get

Or more simply get request transmission method, as to how to get the transmission request with other parameters (such as headers, cookies, etc.), needed to get () for further study and practice

Sending a post request

Also, before you start to learn the following, it is recommended that you first understand what is post request and its characteristics, you learn what follows will be better understood https://www.cnblogs.com/linuxchao/p/linuxchao -http.html

requests to send post request is accomplished by post () method, then we look at its source code

 1 def post(url, data=None, json=None, **kwargs):
 2     r"""Sends a POST request.
 3 
 4     :param url: URL for the new :class:`Request` object.
 5     :param data: (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the :class:`Request`.
 6     :param json: (optional) json data to send in the body of the :class:`Request`.
 7     :param \*\*kwargs: Optional arguments that ``request`` takes.
 8     :return: :class:`Response ` object
 9     :rtype: requests.Response
10     """
11 
12     return request('post', url, data=data, json=json, **kwargs)

By source we can find, post () and get (), as there is only one url parameter must be passed, and still others may not pass, but which data and parameter is json post () method is the most important parameter, below say about when to use the data, when to use json

data

Most post request interface default support type Content-Type is application / x-www-form-urlencoded, it tells us that the interface parameters of the request form needs to pass a form, then we tend to pass form form by constructing a dictionary ,

So when we submit form form data to the server can use parameters, it receives a dictionary type data, stored in the request body, and then transmitted to the server (dictionary type parameter is required)

json

First of all you need to support content_type access interface for the data type application / json format, then you can pass through the interface parameters json this parameter (parameter can be a dictionary can also be a json type)

Let us look at examples send a post request, using the data argument

"""
------------------------------------
@Time : 2019/7/12 10:22
@Auth : linux超
@File : requests_blog.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import requests

login_url = r'http://***/futureloan/mvc/api/member/login'  #

interface address

login_data = {"mobilephone": "13691579841", "pwd": 123456} #

Interface required parameters

response = requests.post(url=login_url, data=login_data) #

Sending a post request

print(response.text) #

Fetch response data

print(response.url) print(response.status_code)

Response Data

{"status":1,"code":"10001","data":null,"msg":"

login successful

"} http://***:8080/futureloan/mvc/api/member/login 200 Process finished with exit code 0

Examples of parameters using the json

"""
------------------------------------
@Time : 2019/7/12 10:22
@Auth : linux超
@File : requests_blog.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import requests

login_url = r'http://***/futureloan/mvc/api/member/login'  #

interface address

login_data = {"mobilephone": "13691579841", "pwd": 123456} #

Interface required parameters

response = requests.post(url=login_url, json=login_data) #

Sending a post request

print(response.text) #

Fetch response data

print(response.url) print(response.status_code)

Response Data

{"status":0,"code":"20103","data":null,"msg":"

The phone number can not be empty

"} http://***/futureloan/mvc/api/member/login 200 Process finished with exit code 0

You can see the use of data parameters and parameter json, acquired the result is not the same, because I used here interface does not support application / json data format, so when you use json parameter passing parameters, the server is unable to resolve data , will not return the correct results

So for when to use data parameters, when to use json parameters need be selected according to the actual data types supported by the interface

params and data difference

I have already said get data parameters params parameters and post the request of the request, then these two parameters in the end what difference does it make?

1. get request transmission, since there is no request body get request, the request url parameter can only follow after the address, and the server can only be obtained by analyzing request url parameters, so get () method can only be used when the get request sent parameter params, it will appends the default parameters of the request to the url

When the user needs to submit some data 2. Generally, the requests are generally for the post request, submit a form post request form, then we can construct a data dictionary format, using data transmission parameters, the request is due to the post body there is a request, and the request parameters can be stored in the request body, only the server request parameters obtained by analyzing the contents of the request body, you can not use the post request interface parameter params pass only use data, json, file, etc. , data request parameters into parameter will request body

Session class Magical

Practice, we often encounter the need to maintain a certain status, in order to follow-up testing of the interface, such as: recharge interface, it requires the user to log in and remain logged in to recharge, then how to solve this case it? This use to Session class requests a library, Session can hold request, as we visit a Web site, as long as we can after login browse any page on the site, look at the following examples

"""
------------------------------------
@Time : 2019/7/12 10:22
@Auth : linux超
@File : requests_blog.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import requests

login_url = r'http://***/futureloan/mvc/api/member/login'  #

Login interface address

login_data = {"mobilephone": "13691579841", "pwd": 123456} #

Interface required parameters

response_login = requests.post(url=login_url, data=login_data) #

Send a post request login

print(response_login.text) recharge_url = r'http://***/futureloan/mvc/api/member/recharge' #

Recharge interface address

recharge_data = {"mobilephone": "13691579841", "amount": 10000.00} #

Interface required parameters

response_recharge = requests.post(url=recharge_url, data=recharge_data) #

Transmission request, begins recharging

print(response_recharge.text)

Results of the

{"status":1,"code":"10001","data":null,"msg":"

login successful

"} {"status":0,"code":null,"data":null,"msg":"

Sorry, please login.

"} Process finished with exit code 0

Can be found, it has already logged on before us, but recharge Shique failed, reason is directly reauests when to send the request, and does not keep the current state (which is the HTTP requests defects), and now we like to use the Session again sending a charge request, modify the code

"""
------------------------------------
@Time : 2019/7/12 10:22
@Auth : linux超
@File : requests_blog.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
import requests


request = requests.Session()  #

Session Initialization

login_url = r'http://***/futureloan/mvc/api/member/login' #

Login interface address

login_data = {"mobilephone": "13691579841", "pwd": 123456} #

Interface required parameters

response_login = request.request(method='post', url=login_url, data=login_data) #

Send a post request login

print(response_login.text) recharge_url = r'http://***/futureloan/mvc/api/member/recharge' #

Recharge interface address

recharge_data = {"mobilephone": "13691579841", "amount": 10000.00} #

Interface required parameters

response_recharge = request.request(method='post', url=recharge_url, data=recharge_data) #

Transmission request, begins recharging

print(response_recharge.text)

Results of the

{"status":1,"code":"10001","data":null,"msg":"

login successful

"} {"status":1,"code":"10001","data":
{"id":5451,"regname":"test","pwd":"E10ADC3949BA59ABBE56E057F20F883E","mobilephone":"13691579841","leaveamount":"15000.00","type":"1","regtime":"2019-05-26 19:08:44.0"},

"msg":"

Successful recharge

"} Process finished with exit code 0

Can be found, we use the Session object to send a charge request is successful. Well, this is what causes it?

Simply put, when we first request to the server to get response information will include a set-cookie field, save the cookies we log information, and if we want to maintain this status, then you need to take when accessing the server again the cookies passed to the server in order to maintain this status.

When we use the Session object sends a request, Session will automatically help us to complete the process described above, Session will automatically about cookies passed to the server without we add cookies manually request parameters, thus maintaining the status log, follow-up dependent operations can be performed normally

Simple encapsulation reqests

Some people may ask, requests the library has been packaged well, directly on the line, why should own package once?

The first through the package, I can direct all requests to use a dictionary to pass parameters unity

For example, the data interface request we need test data is often stored in excel sheet inside, then we get to the rear is a string, the string is not passed as a request parameter, so every time I have to do data conversion, before being passed to the interface, in order to save this process, I just need to package this process in my requests to, data is automatically taken after each treatment to me

Second. When I want to keep a state, do not want to initialize each time a Session object, then I can put it inside the package to my requests, after a direct call to

Let’s look at the package code

 1 """
 2 ------------------------------------
 3 @Time : 2019/7/12 16:14
 4 @Auth : linux超
 5 @File : sendrequests.py
 6 @IDE  : PyCharm
 7 @Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
 8 @QQ   : [email protected]
 9 @GROUP: 878565760
10 ------------------------------------
11 """
12 import json
13 import requests
14 
15 
16 class HttpRequests(object):
17     """
18     eg: request = HttpRequests()
19         response = request(method, url, data)
20         or
21         response = request.send_request(method, url, data)
22         print(response.text)
23     """
24     def __init__(self):
25         self.session = requests.Session()
26 
27     def send_request(self, method, url, params_type='form', data=None, **kwargs):
28         method = method.upper()
29         params_type = params_type.upper()
30         if isinstance(data, str):
31             try:
32                 data = json.loads(data)
33             except Exception:
34                     data = eval(data)
35         if 'GET' == method:
36             response = self.session.request(method=method, url=url, params=data, **kwargs)
37         elif 'POST' == method:
38             if params_type == 'FORM':  #

Form data transmission, data transmission parameters using

39 response = self.session.request(method=method, url=url, data=data, **kwargs) 40 elif params_type == 'JSON': #

If the interface supports application / json type, transmission parameters using json

41 response = self.session.request(method=method, url=url, json=data, **kwargs) 42 else: #

If the interface requires delivery of other types of data such as uploading a file, a request to call the following method

43 response = self.session.request(method=method, url=url, **kwargs) 44 #

If the request mode and non-post will get an error, of course, you can continue to add other request methods

45 else: 46 raise ValueError('request method "{}" error ! please check'.format(method)) 47 return response 48 49 def __call__(self, method, url, params_type='form', data=None, **kwargs): 50 return self.send_request(method, url, 51 params_type=params_type, 52 data=data, 53 **kwargs) 54 55 def close_session(self): 56 self.session.close() 57 try: 58 del self.session.cookies['JSESSIONID'] 59 except: 60 pass 61 62 63 request = HttpRequests() 64 65 66 if __name__ == '__main__': 67 pass

The package only for the get request and post requests, of course, you can also add put, delete, etc. after the request line 32, to achieve a manner more requests

30-34 explain the line: these lines to the data and to convert json string type data format dictionary (dictionary by using the parameter-passing interface) and may be some special treatment in the form of, for example takes the following form

‘{ “Mobilephone”: None, “pwd”: null}’ # string type, but that is not in the form of a string json, nor is the type of string dictionary, the dictionary because there is no null

Packaging and testing

Now we use packaged good way to test and recharge interface send request login

"""
------------------------------------
@Time : 2019/7/12 16:16
@Auth : linux超
@File : test_requests.py
@IDE  : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
@QQ   : [email protected]
@GROUP: 878565760
------------------------------------
"""
from sendrequests import request
import unittest


class TestRequests(unittest.TestCase):

    #

Login interface address

login_url = r'http://***/futureloan/mvc/api/member/login' #

Login interface test data

login_test_value = '{"mobilephone": "13691579841", "pwd": 123456}' #

Recharge interface address

recharge_url = r'http://***/futureloan/mvc/api/member/recharge' #

Top-interface test data

recharge_test_value = {"mobilephone": "13691579841", "amount": 10000.00} def test_login_api(self): """

The login interface test

""" response = request('get', url=self.login_url, data=self.login_test_value) self.assertTrue(response.json()["code"] == "10001") print("

Login interface it is testing through

") def test_recharge_api(self): """

Recharge Interface test

""" response = request('get', url=self.login_url, data=self.login_test_value) try: #

Recharge interfaces need to be logged in to recharge

self.assertTrue(response.json()["code"] == '10001') except AssertionError as e: print('

Login failed!

') raise e else: response = request('post', url=self.recharge_url, data=self.recharge_test_value) self.assertTrue(response.json()["code"] == "10001") print("

Top-interface testing through

") if __name__ == '__main__': unittest.main()

Test Results

登录接口测试通过
..
充值接口测试通过
----------------------------------------------------------------------
Ran 2 tests in 0.570s

OK

Process finished with exit code 0

ok, the test performed by the code, that we do not what packaging problems, and can be sent normally get and post requests, the test data can solve the problems and needs interface dependence problem

to sum up

Finally, we come to sum up all the knowledge related to this article and you need to know

Method 1. requests and sending requests post get request

get(url, params=None, **kwargs)
post(url, data=None, json=None, **kwargs)

2. parmas difference parameters and data parameters

Because no request body get request, post request is requested body

When using params parameters, default parameters will be appended to the url, so when using the get request transmission parameter params

When using data parameter, the parameter stored in the request body, the transmission can not be used during post params request, data should be used, unless the interface, and support and get support post, get request the same parameter data can not be used

3. How to use problem solving Seesion interfaces on hold

Session Initialization example, through this example calling request () method sends a request

4. The most important packaging methods, and learn how to use this package the

Mainly for get and post request interface

Leave a Reply