首页 > 解决方案 > 如何放心地将布尔值作为 json 正文发送

问题描述

我有一个 PUT 服务,其中请求正文为真或假。该服务在 Postman 中运行良好,Content-Type 为 application/json。但是当我尝试使用 Rest-Assured 时它失败了,并且我收到错误消息“A JSONObject 文本必须以 '{' 开头”

下面是我的代码:

RestAssured.given()
                .contentType(ContentType.JSON)
                .accept(ContentType.ANY)
                .body(true)
                .put(sUrl)
                .then().log().ifError()
                .statusCode(Matchers.greaterThanOrEqualTo(200))
                .statusCode(Matchers.lessThanOrEqualTo(299))
                .extract()
                .response();

如果您知道任何解决方案,请告诉我。

标签: javaautomated-testsrest-assured

解决方案


单独的原语不是有效的 json,您已指定它是 via contentType(ContentType.JSON)

主体必须是对象/映射或对象/映射数组。

尝试

.body(Map.of("value", true))

推荐阅读