[英]How to read request body from a list in java and hit the API using Rest Assured?
[英]REST Assured request is not read properly by JSON api
我正在發送一些字符串,例如:
private final String test = "{\"data\":{\"type\":\"test\",\"attributes\":{\"color\":\"yellow\",\"name\":\"TestN\"}}}";
通過放心
given()
.header("Origin", "http://localhost:5000")
.header("Accept-Encoding", "gzip, deflate, br")
.header("Accept-Language", "pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7")
.header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
.header("Content-Type", "application/vnd.api+json")
.header("Accept", "application/vnd.api+json")
.header("Cookie", "xxxxxx")
.header("Connection", "keep-alive")
.header("Cache-Control", "no-cache")
.header("Host", "localhost:4400")
.body(test).with()
.log().everything()
.when()
.post(base + "test-endpoint")
.then().statusCode(201);
不幸的是,API 響應為 500。我通過 Postman 發送相同的請求,它運行良好。 唯一的區別是“屁股”部分。 在郵遞員請求之后,它看起來像:
assigns: %{
doc: %Jabbax.Document{
data: %Jabbax.Document.Resource{
attributes: %{"appointment_color" => "yellow", "name" => "TestN"},
id: nil,
links: %{},
meta: %{},
relationships: %{},
type: "test"
},
errors: [],
included: [],
jsonapi: %{version: "1.0"},
links: %{},
meta: %{}
}
},
在 Rest Assured 請求之后它是空的:
assigns: %{},
添加了所有標頭,我嘗試將其作為從 .json 文件解析的字符串發送。 一切都給出了相同的結果。 有人知道可能是什么問題嗎?
您必須包括內容類型
標題(“內容類型”,“應用程序/ JSON”)
另外,statusCode值應為“ 201”而不是“ 200”
given()
.header("Content-Type","application/json")
.body(test).with()
.log().everything()
.when()
.post(base + "test-endpoint")
.then().statusCode(201);
提示是REST Assured添加了字符集信息-此處描述了類似的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.