0

我正在使用https://github.com/jayway/rest-assured(版本 3.2.1)来测试 REST API,但我在设置 Content-Type 时遇到问题。

我正在做以下测试,

@Test
    public void testSendContentType(){
        String str = givenThat()
                .when()
                .contentType(ContentType.URLENC)
                .get(URL).asString();
        System.out.println(str);
    }

执行测试后,我设置了以下标题。

捕获的标头

Expires: Mon, 30 Jun 2014 14:33:21 GMT
Vary: Accept-Encoding
Content-Encoding: gzip
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json;charset=utf-8

我调试并查看了代码,但我无法理解内容类型是如何设置的,以及为什么它没有设置我提供的内容类型。

我也尝试了以下但没有奏效

.header("Content-Type", ""application/x-www-form-urlencoded")
4

1 回答 1

0

您应该能够使用以下方法设置内容类型:

given().contentType(ContentType.URLENC). .. 

但是,为 GET 请求使用 x-www-form-urlencoded 感觉很奇怪,因为 GET 请求不应该有正文。通常 REST Assured 会发现,如果您在请求中提供表单参数,它应该使用 x-www-form-urlencoded。

于 2014-07-08T06:18:03.903 回答