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"
# }

 

 

转载于:https://www.cnblogs.com/cindy-cindy/p/8320583.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值