接口請求常見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