Python接口自动化--requests 2

Posted 小女子的测试之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python接口自动化--requests 2相关的知识,希望对你有一定的参考价值。

# _*_ encoding:utf-8 _*_
import json

import requests

#post请求
payload = {"cindy":"hello world",
           "python":"1078370383"}

r = requests.post(http://httpbin.org/post,data=payload)
print (r.text)
#输出结果,data数据传输到form里面
# {
#   "args": {},
#   "data": "",
#   "files": {},
#   "form": {
#     "cindy": "hello world",
#     "python": "1078370383"
#   },
#   "headers": {
#     "Accept": "*/*",
#     "Accept-Encoding": "gzip, deflate",
#     "Connection": "close",
#     "Content-Length": "35",
#     "Content-Type": "application/x-www-form-urlencoded",
#     "Host": "httpbin.org",
#     "User-Agent": "python-requests/2.18.4"
#   },
#   "json": null,
#   "origin": "211.140.31.50",
#   "url": "http://httpbin.org/post"
# }

#把payload转换为json格式,post的body是json类型
data_json = json.dumps(payload)
r1 = requests.post(http://httpbin.org/post,data=data_json)
print (r1.text)
#输出结果,返回结果传回到data里
# {
#   "args": {},
#   "data": "{\"python\": \"1078370383\", \"cindy\": \"hello world\"}",
#   "files": {},
#   "form": {},
#   "headers": {
#     "Accept": "*/*",
#     "Accept-Encoding": "gzip, deflate",
#     "Connection": "close",
#     "Content-Length": "48",
#     "Host": "httpbin.org",
#     "User-Agent": "python-requests/2.18.4"
#   },
#   "json": {
#     "cindy": "hello world",
#     "python": "1078370383"
#   },
#   "origin": "211.140.31.50",
#   "url": "http://httpbin.org/post"
# }

 

以上是关于Python接口自动化--requests 2的主要内容,如果未能解决你的问题,请参考以下文章

Python接口自动化--requests 2

python+requests接口自动化测试实战

python接口自动化-requests库requests库安装

python+requests接口自动化4. 接口实现文件(图片)上传

python+requests接口自动化4. 接口实现文件(图片)上传

python3+requests实现接口自动化1