接口请求常见get和post传递参数


如果是传递得json格式得参数:

p={"name":"john","age":17}
response=requests.post("http://localhost:5000/post_params",json=p)
print(response.text)
###输出:
{"name": "john", "age": 17}

如果是以html中form得形式传递参数

response=requests.post("http://localhost:5000/post_params",data=p)
print(response.text)

如果form传递得是json格式需要以下面得方式

import json
response=requests.post("http://localhost:5000/post_params",data=json.dumps(p))
print(response.text)

说明:
需要将字典行驶得参数转换为json格式,再进行传递








1.get请求传递参数

params={"name":"john","age":17}
response=requests.get("http://localhost:5000/get_params",params=params)
print(response.text)
print(response.url)
##输出
{"name": "john", "age": "17"}
http://localhost:5000/get_params?name=john&age=17

说明:
这里做了三件事,先定义一个参数字典p,然后将参数字典以params参数传递给get方法,最后将响应信息打印出来,从响应信息可以看到参数被正确得传递进去,requests帮我们把参数组合到了url里面

2.post请求传递参数

如果是传递得json格式得参数

p={"name":"john","age":17}
response=requests.post("http://localhost:5000/post_params",json=p)
print(response.text)
###输出:
{"name": "john", "age": 17}

如果是以html中form得形式传递参数

response=requests.post("http://localhost:5000/post_params",data=p)
print(response.text)

如果form传递得是json格式需要以下面得方式

import json
response=requests.post("http://localhost:5000/post_params",data=json.dumps(p))
print(response.text)

3.

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM