python 接口库_python接口测试之requests库(一)

论坛 期权论坛 脚本     
已经匿名di用户   2022-4-13 16:43   1467   0

print dir(requests)

三、模拟发送get请求

1、发送不带参数的get请求

r = requests.get('https://api.github.com/events')

返回的是一个response对象,该对象中包含的内容,通过dir(r)

2、发送带参数的get请求,如get请求链接 http://httpbin.org/get?key2=value2&key1=value1

需要使用params关键字,将?后的参数写入params参数中,如下:

import requests

r = requests.get('http://httpbin.org/get',params={'key1': 'value1', 'key2': 'value2'})

print r.url

参数也可以分离出来,如下

import requests

payload = {'key1': 'value1', 'key2': 'value2'}

r = requests.get('http://httpbin.org/get',params=payload)

print r.url

以上的运行结果都是:

3、发送带请求头的get请求,需要传递headers参数

import requests

url='https://api.github.com/some/endpoint'

headers = {'user-agent': 'my-app/0.0.1'}

r = requests.get(url,headers=headers)

四、模拟发送post请求

1、发送post请求时,需要把请求参数放在data参数中

url = 'http://httpbin.org/post'

payload = {'key1':'value1','key2':'value2'}

r = requests.post(url,data = payload)

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP