Requests interface test Basics

Time:2021-11-29
Send HTTP request
Import the requests library and set the interface address of the test as a variable
import requests
base_url = 'http://httpbin.org'
Get request
r =requests.get(base_url+'/get')
print(r.status_code)
Post request
r=requests.post(base_url+'/post')
print(r.status_code)
Put request
r=requests.put(base_url+'/put')
print(r.status_code)
Delete request
r=requests.delete(base_url+'/delete')
print(r.status_code)
Get pass in parameters
param_data = {'user':'zxw','password':'666'}
r =requests.get(base_url+'/get',params=param_data)
print(r.url)
print(r.status_code)
Post pass in parameters
form_data = {'user':'zxw','password':'666'}
r=requests.post(base_url+'/post',data=form_data)
print(r.text)
Header request header settings
headers = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'}
r = requests.get("https://www.zhihu.com/",headers=headers)
print(r.status_code)
print(r.text)
Cookies settings
Items() function: turn the list into tuples
cookie={'user':'zxc'}
s = requests.Session()
r = s.get(base_url+'/cookies/set/user/51zxw')
print(r.text)
r = s.get(base_url+'/cookies')
print(r.text)
r = requests.get('http://www.baidu.com')
print(r.cookies)
print(type(r.cookies))
for key,value in r.cookies.items():
    print(key+':'+value)
Timeout setting
Timeout is the waiting time. If there is no response after the waiting time, an error will be reported. The following code is set to a particularly short time.
cookie={'user':'zxc'}
r = requests.get(base_url+'/cookies',cookies=cookie,timeout=0.003)
print(r.text)
Upload file
file = {'file':open('C:\\Users\\sam\\Desktop\\scre\\test.jpg','rb')}
r = requests.post(base_url+'/post',files=file)
print(r.text)
Turn off authentication SSL
r = requests.get('https://www.12306.cn',verify=False)
print(r.text)
Proxy settings
proxies = {'http':'http://219.141.153.41:80'}
r = requests.get(base_url+'/get',proxies=proxies)
print(r.text)
identity authentication
from requests.auth import HTTPBasicAuth
from requests.auth import HTTPDigestAuth
#Identity authentication - basicauth
r = requests.get(base_url+'/basic-auth/51zxw/8888',auth=HTTPBasicAuth('51zxw','8888'))
print(r.text)
#Identity authentication digestauth
r = requests.get(base_url+'/digest-auth/auth/51zxw/8888',auth=HTTPDigestAuth('51zxw','8888'))
print(r.text)
Streaming request
import json
r = requests.get(base_url+'/stream/10',stream=True)

if r.encoding is None:
    r.encoding = 'utf-8'

for line in r.iter_lines(decode_unicode=True):
    if line:
        data = json.loads(line)
        print(data['id'])
Interface test template
import requests
from urllib import parse
import json

#G) construct interface test data
Data = {'city': 'Beijing'}
city = parse.urlencode(data).encode('utf-8')
url = 'http://t.weather.sojson.com/api/weather/city/101030100'

#Send request
r = requests.get(url,params=city)

#Convert the returned result to JSON type
response_data = r.json()

#Obtain the date, corresponding information, status and city respectively
print(response_data['date'])
print(response_data['message'])
print(response_data['status'])
#print(response_data['cityInfo'][0]['city'])

#Get the weather of the day
print(response_data['data']['forecast'][0]['date'])
print(response_data['data']['forecast'][0]['type'])
print(response_data['data']['forecast'][0]['high'])
print(response_data['data']['forecast'][0]['low'])

Above transferred from:https://blog.csdn.net/m0_37193944/article/details/88725946

Problems encountered

Error 1: requests.exceptions.sslerror: httpsconnectionpool (host =’console. Box. Lenovo. Com ‘, port = 443): Max retries exceeded with URL: / V2 / user / login_ new (Caused by SSLError(SSLCertVerificationError(1, ‘[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)’)))

resolvent

#Add a parameter verify = false to the request
r = requests.post(url, data, verify=False)

Error 2: unverified HTTPS request is being made to host ‘127.0.0.1’. Adding certificate verification is strongly advised

resolvent

#Add the following statement before the request
requests.packages.urllib3.disable_warnings()
#Request
r = requests.post(url,data)