json 是一種輕量級的傳輸資料格式,用于資料互動,json 請求型別的請求頭中的 Content-Type 對應為 application/json ,碰到這種型別的介面,使用 Java 的 REST Assured 或者 Python 的 Requests 均可解決,
實戰演示
在 Python 中,使用 json 關鍵字引數發送 json 請求并傳遞請求體資訊,
>>> import requests
>>> r = requests.post(
'https://httpbin.ceshiren.com/post',
json = {'key':'value'})
>>> r.request.headers
{'User-Agent': 'python-requests/2.22.0',
'Accept-Encoding': 'gzip, deflate',\
'Accept': '*/*', 'Connection': 'keep-alive',
'Content-Length': '16',\
'Content-Type': 'application/json'}
如果請求的引數選擇是json ,那么Content-Type 自動變為application/json ,
在 Java 中,使用contentType()方法添加請求頭資訊,使用body()方法添加請求體資訊,
import static org.hamcrest.core.IsEqual.equalTo;
import static io.restassured.RestAssured.*;
public class Requests {
public static void main(String[] args) {
String jsonData = "https://www.cnblogs.com/chengzi-ceba/p/{/"key\": \"value\"}";
//定義請求頭資訊的contentType為application/json
given().contentType("application/json").
body(jsonData).
when().
post("https://httpbin.ceshiren.com/post").
then().body("json.key", equalTo("value")).log().all();
}
}
``
喜歡軟體測驗的小伙伴們,如果我的博客對你有幫助、如果你喜歡我的博客內容,請 “點贊” “評論” “收藏” 一鍵三連哦,[更多技術文章](https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=bokeyuan×tamp=1652247797&author=BB)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/501077.html
標籤:其他
上一篇:今天公司來了個拿 30K 出來的測驗,算是見識到了基礎的天花板
下一篇:軟體測驗基礎理論(2)
