接口使用post提交的數(shù)據(jù)一般為Json或者Xml桑腮,Json使用的最多逸吵。以下例子說明requests模塊如何使用post方法提交Json數(shù)據(jù)(例子代碼不可運行只做舉例)
# -*- coding: UTF-8 -*-
import requests
username = 'anxiaolong'
password = '123456'
test_url = 'http://www.reibang.com/sign_in' # 接口地址
datalist = {'uname': username, 'pwd': password} # 將參數(shù)添加到需求post的data中
head = {"Content-Type": "application/Json"} # 定義頭部摆寄,告訴服務(wù)器提交的是Jason或者是Xml
response = requests.post(test_url, datas=datalist, headers=head) # 發(fā)起一個請求罗售,使用post方法
result = response.text
print(result)
get和post都是是http協(xié)議提供請求方式伶棒,get和post的在接口測試過程中一般不涉及到傳遞大量數(shù)據(jù)文件和表單,所以差別不大项戴。不過post的安全性相對get要好些形帮,所有目前絕大多數(shù)接口數(shù)據(jù)都是通過post方式傳遞。要想具體的掌握get和post的區(qū)別周叮,可以參閱:get和post的區(qū)別