JSON api无法正确读取REST Assured请求

问题描述 投票:0回答:2

我正在发送一些字符串,例如:

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.我通过邮递员发送相同的请求,它的工作完美。唯一的区别是“assings”部分。在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: %{}
 }
},

在休息后确认请求它是空的:

assigns: %{},

添加了所有标题,我尝试将其作为从.json文件解析的字符串发送。一切都给出了相同的结果。有人知道会出现什么问题吗?

java json postman rest-assured json-api
2个回答
0
投票

您必须包含内容类型

标题(“内容类型”,“应用程序/ JSON”)

此外,statusCode值应为“201”而不是“200”

given()
.header("Content-Type","application/json")
.body(test).with()
.log().everything()
.when()
.post(base + "test-endpoint")
.then().statusCode(201);

0
投票

线索是REST Assured添加了一个字符集信息 - 此处描述了类似的问题。

© www.soinside.com 2019 - 2024. All rights reserved.