python接口测试-get请求

一、环境准备

  (1)requests安装

    pip install request安装

    

    安装完完成会提示

    Successfully installed certifi-2020.12.5 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.3

    requests使用方法可以参考https://requests.readthedocs.io/zh_CN/latest/index.html

  二、使用requests发起不带参数的get请求

    我这是直接在编辑器中使用的,python编辑器可根据自身习惯选择,我这里用的是VSCode

    

1 #导入requests
2 import requests
3 #发起一个get请求,请求我自己的博客
4 r = requests.get("https://www.cnblogs.com/qkblogs/")
5 #返回错误码
6 print(r.status_code)
7 #返回解码后的内容
8 print(r.content)

  

  三、使用request发起带参数的get请求

  

import requests
#定义协议格式
http_protocol = "http://"
#定义id
param = {"id":"12345678000000"}
#定义host地址
host = "192.168.11.112:6262"
#定义接口地址
uri = "/host/protected/getId/"

print(uri)
#定义header
header = {
    "appId":"MB-UZHSH-0000",
    "appVersion":"01.00.00.00000",
    "clientId":"ufmtest123",
    "sequenceId": "20161020153428000015",
    "accessToken": "TGT37M3VIU8CA6N82X20JZXFJYX7Q0",
    "language": "zh-cn",
    "timezone": "+8",
    "appKey": "f50c76fbc8271d361e1f6b5973f54585",
    "Content-Encoding": "utf-8",
    "Content-type": "application/json",
    "Content-Length": "0",
    "Host": "192.168.150.67:6220",
    "User-Agent": "Apache-HttpClient/4.5.2 (Java/1.8.0_251)",
    "sign": "e27ae421cd857717383d1de74f7333d3f21b4bdf05cd4374f8b2eb511501fc78"
    }
#发起请求
r = requests.request("GET",url =http_protocol+host+uri,headers = header,params = param)

print(r.status_code)

print(r.text)

Requests库的主要方法:requests.request为requests.get和requests.post两个的汇总,只是需要传方法-----转载

 

比如:

r=requests.request('GET',url,**kwargs)

r = requests.request('POST', url, **kwargs)

r = requests.request('PUT', url, **kwargs) 

r = requests.request('delete', url, **kwargs)

 

 

posted @ 2021-03-09 15:57  柚子李子  阅读(458)  评论(0编辑  收藏  举报