Python encountered problems when sending http request

1. The error message is "ERROR 'str' object has no attribute 'endwith'" , and the investigation found that the names of the endswith method were written incorrectly, and s was missing and it was written as 'endwith'

                if interface_url.endswith('?'):
                    req_url = interface_url + temp_interface_param
                else:
                    req_url = interface_url + '?' + temp_interface_param

2. The error message is "ERROR request () got an unexpected keyword argument 'header" , the investigation found that the parameter name headers was written as header

response = requests.get(url=req_url, headers=header_data, verify=False, timeout=10)

3. The error message is "ERROR No connection adapters were found for 'test.XXX.xxx/xxx/'" , the investigation found that the URL lacks "http: //", plus solve the problem

4. Send an HTTP request. When the request method is post, the url and parameters are correct, but the error is still reported. The investigation found that when using Python to send the request, the parameters were not passed in, resulting in an error;

Further investigation revealed that the content-type value of the request header parameter type is not set. There are four commonly used content-types, as follows:

(1) application / x-www-form-urlencoded: is the most common data type, usually indicating that the requested data type is a key-value pair type

(2) application / json: the requested data type is json format

(3) multipart / form-data: usually used to upload files

(4) application / xml: data format is xml format

After adding 'content-type': 'application / x-www-form-urlencoded' data in the request header, the parameters passed follow the corresponding format, and the HTTP request can return the data normally

Request header content-type value reference: https://www.jianshu.com/p/f59b04ffea61

Guess you like

Origin www.cnblogs.com/dancy0dante/p/12686754.html