当前位置:网站首页>In depth analysis and encapsulation call of requests

In depth analysis and encapsulation call of requests

2022-07-06 09:01:00 Automated test seventh uncle

Preface

Speaking of python send out HTTP Request for interface automation testing , The first thing that flashed through my mind might be requests The library , Of course python There are many modules that can send HTTP request , Including native modules http.client,urllib2 etc. , But because the native module is too complex , Cumbersome use , that requests The library was born , It is also one of the more popular interface automation testing tools at this stage .

 

requests It's a third-party library , Encapsulates the HTTP All methods requested , Easy to use , The whole process of sending network requests can be completed by calling corresponding methods according to different request modes , So today, let's talk about requests How libraries are used , Before we start, let's talk about today's main content :

1. requets How to send get request

2. requests How to send post request

3. params Parameters and data Parameter difference

4. requests In the library Session The wonderful use of class

5. in the light of get Request and post request , Yes requests Simple encapsulation

One 、 install

Usually , Before we use third-party libraries , All need to be installed first

cmd Carry out orders pip install requests Then you can introduce the module and use the methods it provides

import requests

Two 、 send out get request

requests By calling get() Method to complete the sending get Requested , that , In control requests How the library sends get Request before , You should also briefly understand what is get request

Usually before we learn how to use a method , We need to know which parameters this method needs ? What is the parameter type ? Then let's analyze it first get() Method source code

 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 <Response>` object
 9     :rtype: requests.Response
10     """
11 
12     kwargs.setdefault('allow_redirects', True)
13     return request('get', url, params=params, **kwargs)

This is it. get Method source code , You should be able to find ,get() Method has only one required parameter url, Other parameters are not required , So what do other parameters do ?

3、 ... and 、params

For this parameter , It can be a dictionary , It can also be a list of nested tuples , be based on get Characteristics of the request , Request parameters can be directly followed by URL After that , With ? Start with key=value Form transfer ( Multiple parameters use & The symbols are divided ), But to make a clear distinction URL And parameters , You need to use params Parameter passing

**kwargs: Other keyword parameters , No introduction for now

So let's see 2 A simple example , Give it a try reauets adopt get() Method sends a with no parameters get The process of requests and requests with parameters

adopt get() Method to send get Request to visit the home page of the blog Park

"""
import requests  #  Import requests modular 


response = requests.get('https://www.cnblogs.com/')  #  send out get request 
print(response.text)  #  Get response data 

The response data

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="referrer" content="always" />
    <title> The headline number  -  Code changes the world </title>
        <meta name="keywords" content=" developer , The headline number , developer , Program the ape , Luca brasi program , geek , Programming , Code , Open source ,IT Website ,Developer,Programmer,Coder,Geek, Technology community " />
            <meta name="description" content=" Is a knowledge sharing community for developers . Since its creation , Has been committed to and focused on building a pure technology exchange community for developers , Promote and help developers share knowledge through the Internet , So that more developers can benefit . The mission is to help developers change the world with code ." />
    <link rel="shortcut icon" href="//common.cnblogs.com/favicon.ico" type="image/x-icon" />
    <link rel="Stylesheet" type="text/css" href="/bundles/aggsite.css?v=IlEZk4Ic2eCzcO6r0s4bGm62FAo8VZI-US_0EqUe2Bk1" />
        <link id="RSSLink" title="RSS" type="application/rss+xml" rel="alternate" href="http://feed.cnblogs.com/blog/sitehome/rss" />
    <script src="//common.cnblogs.com/scripts/jquery-2.2.0.min.js" type="text/javascript"></script>
    <script src="/bundles/aggsite.js?v=cE0bqImLWsEG3gZXOupKxj5tj_ukK7pLeSd73DHZOT81" type="text/javascript"></script>
    <script async='async' src='https://www.googletagservices.com/tag/js/gpt.js'></script>
    <script>
        var googletag = googletag || {};
        googletag.cmd = googletag.cmd || [];
    </script>

Here I only intercepted part of the response data , The response data is actually the home page HTML Source code

You can see that the entire request process can be completed with only one line of code , adopt response.text Get the response data ( In fact, this process is the same as entering the address of the blog Park in the browser ), Of course, you can also use the following methods to obtain different response data

response.headers  #  Get response header information 
response.cookies  #  Get the returned cookie
response.status_code  #  Get status code 

Send tape params Parametric get request

"""
import requests

login_url = r'http://***/futureloan/mvc/api/member/login'  #  Address of the interface 
login_data = {"mobilephone": "13691579841", "pwd": 123456}  #  Interface required parameters 
response = requests.get(url=login_url, params=login_data)  #  send out get request 
print(response.text)  #  Get response data 
print(response.url)

The 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 pass a dictionary to params Parameters , You can complete the with parameters get request , And get the response data

Four 、 Be careful

1. In our daily work, we'd better transfer the dictionary as the interface data , If the data type is json When sending a request, it needs to be converted into a dictionary

2. As long as you send get request , And don't want to pass url Directly transfer interface parameters by splicing , Then only use params Parameters to transmit ( If you use data,json When waiting for parameters, you will find , The request will not succeed ), Because by params The passed parameters are appended to url after , This is also get Characteristics of the request , So you need to remember : send out get Parameters can only be used when requesting params that will do

The above is just a simple send get Requested method , As for how to send with other parameters get request ( such as headers,cookies etc. ), You need to get() Further research and Practice

5、 ... and 、 send out post request

requests send out post Request is through post() Method to achieve , Then let's take a look at its source code first

 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 <Response>` object
 9     :rtype: requests.Response
10     """
11 
12     return request('post', url, data=data, json=json, **kwargs)

Through the source code, we can find that ,post() and get() equally , There is only one url Parameters are required , Others can still not be passed , But among them data and json The argument is post() The most important parameter in the method , Let's talk about when to use data, When to use json

6、 ... and 、data

majority post The requested interface supports parameter types by default Content-Type by application/x-www-form-urlencoded, It tells us that the requested interface parameters need to pass a form Forms , Then we often construct a dictionary to convey form Form ,

So when we submit to the server form Forms can be used data Parameters , It will receive a dictionary type of data , Stored in the request body , Then send it to the server ( Parameter must be dictionary type )

7、 ... and 、json

First of all, the interface you access needs to support content_type by application/json The data type of the format , Then you can go through json This parameter is used to pass interface parameters ( The parameter can be a dictionary or json type )

Let's take a look at sending post request , Use data Examples of parameters

"""
import requests

login_url = r'http://***/futureloan/mvc/api/member/login'  #  Address of the interface 
login_data = {"mobilephone": "13661577841", "pwd": 123456} #  Interface required parameters 
response = requests.post(url=login_url, data=login_data)  #  send out post request 
print(response.text)  #  Get response data 
print(response.url)
print(response.status_code)

The 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

Use json Parameter instance

"""
import requests

login_url = r'http://***/futureloan/mvc/api/member/login'  #  Address of the interface 
login_data = {"mobilephone": "13691579841", "pwd": 123456} #  Interface required parameters 
response = requests.post(url=login_url, json=login_data)  #  send out post request 
print(response.text)  #  Get response data 
print(response.url)
print(response.status_code)

The response data

{"status":0,"code":"20103","data":null,"msg":" Cell phone number cannot be empty "}
http://***/futureloan/mvc/api/member/login
200

Process finished with exit code 0

You can see the use of data Parameters and json Parameters , The returned results are different , Because the interface I use here does not support application/json data format , So when you use json Parameter when passing parameters , The server cannot parse the data , It will not return the correct result

So when to use data Parameters , When to use json Parameters , It also needs to be selected according to the data type supported by the actual interface

8、 ... and 、params and data difference

That was already said get In the request params Parameters and post In the request data Parameters , So what is the difference between these two parameters ?

1. send out get When asked , because get The request has no body , Request parameters can only follow url After address , And the server can only parse url Get the requested parameters , therefore get() Method to send get Only... Can be used when requesting params Parameters , It appends the requested parameters to by default url After the address

2. Usually, users need to submit some data , The requests sent are generally post request ,post The request will submit a form Forms , Then we can construct data in dictionary format , Use data Parameter passing , because post Requests have a body of requests , And the request parameters are stored in the request body , The server can only obtain the requested parameters by parsing the contents in the request body , therefore post Request cannot use params Pass interface parameters , Only use data,json,file etc. , data Parameters will put the request parameters into the request body

Nine 、Session The wonderful use of class

In practice , We often encounter the need to maintain a certain state , To test subsequent interfaces , for instance : Recharge interface , You need to log in first , And it can only be recharged after being logged in , So how to solve this situation ? That's what we need requests In the library Session The class ,Session You can keep the requested State , Like we visit a website , As long as we log in, we can browse any page on the website , Let's first look at the following examples

"""
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 out post request   Sign in 
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)  #  Send a request , Start recharging 
print(response_recharge.text)

Execution results

{"status":1,"code":"10001","data":null,"msg":" Login successful "}
{"status":0,"code":null,"data":null,"msg":" I'm sorry , Please log in first ."}

Process finished with exit code 0

You can find , We have all logged in before , But the recharge failed , The reason is to use it directly reauests To send a request , It will not remain in its current state ( This is also HTTP Requested defect ), Now we use Session Send recharge request to image again , Modify the code

"""
import requests


request = requests.Session()  #  initialization Session
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 out post request   Sign in 
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)  #  Send a request , Start recharging 
print(response_recharge.text)

Execution results

{"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":" Recharge successful "}

Process finished with exit code 0

You can find , We switch to Session Object to send the recharge request . So what's the reason ?

Simply speaking , When we first requested the server , The response information obtained will contain a set-cookie Field of , Saved our login cookies Information , If we want to maintain this state , You need to bring this when you access the server again cookies Pass it to the server , To maintain this state .

So we use Session When the object sends a request ,Session It will automatically help us complete the above process ,Session Will automatically cookies To the server , There is no need for us to manually add cookies, This keeps the login status , Subsequent dependent operations can be executed normally

Ten 、reqests Simple packaging

Someone will ask. ,requests The library is well encapsulated , Just use it , Why do you have to package it yourself ?

First of all . Through encapsulation , I can directly pass all request parameters in a unified dictionary

such as , The data required by our interface request, that is, the test data, is often saved in excel Inside the watch , Then we get the string type , A string cannot be passed as a request parameter , So I have to do data conversion every time , And then pass it to the interface , To save this process , I just need to encapsulate this process into my requests It's OK , Every time I get the data, it will be processed automatically

second . When I want to maintain a certain state , I don't want to initialize one at a time Session object , Then I can package it into my requests Inside , You can call it directly later

Let's look at the encapsulated code


 """
 import json
 import requests
  
 class HttpRequests(object):
     """
     eg: request = HttpRequests()
         response = request(method, url, data)
        or
         response = request.send_request(method, url, data)
         print(response.text)
     """
     def __init__(self):
         self.session = requests.Session()
 
     def send_request(self, method, url, params_type='form', data=None, **kwargs):
         method = method.upper()
         params_type = params_type.upper()
         if isinstance(data, str):
             try:
                 data = json.loads(data)
             except Exception:
                     data = eval(data)
         if 'GET' == method:
             response = self.session.request(method=method, url=url, params=data, **kwargs)
         elif 'POST' == method:
             if params_type == 'FORM':  #  Send form data , Use data Parameter passing 
                response = self.session.request(method=method, url=url, data=data, **kwargs)
             elif params_type == 'JSON':  #  If the interface supports application/json type , Then use json Parameter passing 
                 response = self.session.request(method=method, url=url, json=data, **kwargs)
             else:  #  If the interface needs to transfer other types of data, such as   Upload files , Call the following request method 
                 response = self.session.request(method=method, url=url, **kwargs)
         #  If the request method is not  get  and post  Will report a mistake , Of course, you can also continue to add other request methods 
         else:
             raise ValueError('request method "{}" error ! please check'.format(method))
         return response
 
     def __call__(self, method, url, params_type='form', data=None, **kwargs):
         return self.send_request(method, url,
                                 params_type=params_type,
                                  data=data,
                                  **kwargs)
 
     def close_session(self):
         self.session.close()
         try:
             del self.session.cookies['JSESSIONID']
         except:
             pass
 
 
 request = HttpRequests()
 
 
 if __name__ == '__main__':
     pass

This package is only for get Request and post request , Of course, you can also put put,delete Wait for the request to be added in 32 After line code , Realize more requests

Explain it. 30-34 Line code : These lines of data are to put json And string type data are converted to dictionary format ( Pass interface parameters by using a dictionary ) And can handle some special forms , For example, the following format

'{"mobilephone": None, "pwd": null}' # String type , But it's not json String of form , Nor is it a dictionary type string , Because there is no null

Xi. Packaging test

Now let's use the encapsulated method to test the request to send the login and recharge interface

"""
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": "13661577841", "pwd": 123456}'

    #  Recharge interface address 
    recharge_url = r'http://***/futureloan/mvc/api/member/recharge'
    #  Recharge interface test data 
    recharge_test_value = {"mobilephone": "13661577841", "amount": 10000.00}

    def test_login_api(self):
        """ Login interface test cases """
        response = request('get', url=self.login_url, data=self.login_test_value)
        self.assertTrue(response.json()["code"] == "10001")
        print(" Login interface test passed ")

    def test_recharge_api(self):
        """ Recharge interface test case """
        response = request('get', url=self.login_url, data=self.login_test_value)
        try:
            #  The recharge interface needs to log in first , 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(" Recharge interface test passed ")

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

test result

 Login interface test passed 
..
 Recharge interface test passed 
----------------------------------------------------------------------
Ran 2 tests in 0.570s

OK

Process finished with exit code 0

ok, Test code execution passed , It shows that there is no problem with our packaging , And it can be sent normally get and post request , It can also solve the problems of test data and interface dependency

summary

Finally, let's summarize all the knowledge points involved in this article and what you need to master

1. requests send out get Request and post Requested method

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

2. parmas Parameters and data Parameter difference

because get Request has no body ,post The request has a request body params When parameters are , By default, parameters are appended to url Back , So send get When requested, use params Parameters , Use data When parameters are , The parameters will be stored in the request body , So send post Cannot use... When requesting params, You should use data, Unless interface and support get And support post, Again get Requests cannot be used either data Parameters

3. How to use Seesion Solve the problem of interface maintaining state

initialization Session example , Call... Through this instance request() Method to send a request

4. One of the most important encapsulation methods , And learn how to use this package

Mainly aimed at get and post Requested interface

Finally, that's all for today's article. My favorite friends can like collection comments and pay attention to it .

 

原网站

版权声明
本文为[Automated test seventh uncle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060856581472.html