ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python接口测试-get请求

2021-03-09 16:01:25  阅读:153  来源: 互联网

标签:get python request 接口 url kwargs print requests


一、环境准备

  (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)

 

 

标签:get,python,request,接口,url,kwargs,print,requests
来源: https://www.cnblogs.com/qkblogs/p/14501377.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有