Python Interface test 401 error analysis and resolution (16)

Source: Internet
Author: User
Tags auth http authentication

The author is boundless

In the interface test, often encountered the client to the server to send a request, the server returned 401 error, then today this article to explain how to analyze and solve the problem in the interface test.

We know that in the status code returned by HTTP, a 401 error indicates that the requested page requires a user name and password. A 401 error can be described in detail as: The client sends the request to the server,

The page needs to verify that the server will return a 401 error, as shown in the following error message:

401 Unauthorized Headers
Content-Type: application/jsonWWW-Authenticate: Basic realm="Authentication Required"Content-Length: 37Server: Werkzeug/0.11.13 Python/2.7.12Date: Wed, 14 Mar 2018 14:57:24 GMT
{  "error": "Unauthorized access"}

According to the wrong information can be obtained, sent requests need to have authenticated authentication information through HTTP, if accessed in the browser, will be directly pop-up need to enter the user name and password pop-up box, see:

Then in the HTTP authentication, often will be used to the authentication method is basic certification and digest certification, we specifically see the Basic authentication method is a popular,

The industry-standard authentication method is specified in HTTP1.0. Mainly refers to the use of the user ID and password using the BASE64 encoding standard encoding, and through the HTTP to transmit the genus,

The server allows users access only if the user ID and password are valid.

In our case, the authentication method used is the Basic authentication method, then when the client sends the request to the server, bring the user information, the request can be successful again.

For basic auth in postman, specify the user name and password, and see the information for the successful request:

Response (20.345s)-http://localhost:5000/hotel/username/ OK Headers
Content-Type: application/jsonContent-Length: 308Server: Werkzeug/0.11.13 Python/2.7.12Date: Wed, 14 Mar 2018 15:09:33 GMT
{  "datas": 
 [    {      
      "check in": "2018-03-08 08:20:10",      
      "check out": "2018-03-09 14:00:00",      
      "identity card": "23012919950425723X",      
      "phone": "13484545190",      
       "room number": "1104",      
       "userid": 1,      
       "username": "李四",      
       "vpl": "京AJ3585"    }  ]}

Of course we can also use requests to easily solve this part, see the implementation of the Code:

# !/usr/bin/env python # -*-coding:utf-8-*- Import   REQUESTSR=requests.get (   'http://localhost:5000/hotel/username/ ' ,   auth= ('wuya','admin') )Print r.text

Or specify httpbasic directly, see the modified code:

#!/usr/bin/
# -*-coding:utf-8-*-
Importfromimport
R='http://localhost:5000/hotel/username/'
Auth=httpbasicauth ('wuya','admin'
Print R.text

Through a case analysis in the HTTP request 403 Error resolution and HTTP status code in 403 How to understand, see Send a request, return the status code, see the following information:

In the above, you can see that the HTTP status code is returned is 403, then how this process, in the HTTP status code, 403 forbidden indicates that access to the requested resource is denied, and the server does not give reasons for why the refusal, such as our browser to access such as the link, See the response from the service side after the visit:

{  "error": "Unauthorized Access"}

See General's content:

Request url:http://localhost:5000/hotel/username/
Request Method:get
Status code:403 FORBIDDEN
Remote address:127.0.0.1:5000

See the contents of the Response Headers section:

Content-length:37
Content-type:application/json
Date:sun, APR 2018 14:04:36 GMT
server:werkzeug/0.11.13 python/2.7.12
Www-authenticate:basic realm= "Authentication Required"

In the information above, the header field, Www-authenticate, is used for HTTP access authentication, which tells the client the authentication scheme (Basic or Digest) used to request the resource specified by the URI. Show Basic, then we get the authentication scheme using the basic Auth, in the use of Postman request, need authentication, authentication, again access, HTTP status code is no longer 403, but 200. See the result of the request after authentication:

In the request, the headers must be taken with authorization, or a 403 error will occur again.

The following implementation uses the Python language to handle 403 of errors, and when requested with authentication, see the code implemented:

#!/usr/bin/env python#-*-coding:utf-8-*-ImportREQUESTSR=requests.get (URL='http://localhost:5000/hotel/username/', auth=('Wuya','Admin'))PrintR.text See the result of printing after executing the code as above: {"datas": [    {      "Check in":"2018-03-08 08:20:10",       "Check out":"2018-03-09 14:00:00",       "identity card":"23012919950425723X",       "Phone":"13484545190",       " the number":"1104",       "userid": 1,       "username":"\u674e\u56db",       "VPL":"\u4eacaj3585"    }  ]}

Python Interface test 401 error analysis and resolution (16)

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.