WebAPI automation interface test 2

Interface Certification:

       Interface server needs through a verification mechanism, this authentication mechanism can be a session or token

       To the session, for example.

            session with the storage server, usually after the user logs on, the server will support the sessionid sent to the client

            The client will take the next sessionid server request, it took the equivalent of tickets

            Server verifies the ticket is valid

            After verification by only allowing the client to access the corresponding interface content

            After every client sends a request should bring sessionid

            The same server every time to verify its validity

       Typically http protocol, sessionid request header is placed inside the cookies

How to get sessionid: two ways

       ① acquire Set-Cookie request header by logging Interface

            resp = requests.post (url, data = payload) # resp response object is first acquired

            cookie = resp.headers [ 'Set-Cookie'] # Set-Cookie header value is then acquired request

            sessionid = cookie.split ( ';') [0] .split ( '=') [1] # acquired last character string dividing method sessionid

       ② direct access by login response object returned interface

            resp = requests.post(url, data=payload)

            cookies = resp.cookies # directly to the cookie information

            sessionid = cookies [ 'sessionid'] # is then taken from the cookie value to the sessionid

How to add cookie method: three ways

       ① directly added to the cookie request in advance

            h1 = {'Cookie': 'sessionid=imk4esz7jep90gcbt3oo790ex3srt5dc'}

            requests.get(url, headers=h1, params=payload)

       ② tell requests, let me add add

           cookie = {'Cookie': 'sessionid=imk4esz7jep90gcbt3oo790ex3srt5dc'}

           requests.get(url, cookies=cookie, params=payload)

       ③ login interface contains the cookies information can be obtained directly

           payload = {'username': 'xxxx', 'password': 'xxxx'}

           resp1 = requests.post ( 'http: // localhost / api / mgr / loginReq', data = payload) # Respl acquired response object which contains the information cookies

           resp2 = requests.get ( 'http: // localhost / api / mgr / sq_mgr /', cookies = resp1.cookies, params = payload) # request when making another interface, direct access to cookies information registration interface

Guess you like

Origin www.cnblogs.com/peipei-Study/p/12024053.html