如何放心发送Content-Length,因为遇到“Content-Length header已经存在”的错误

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

我正在使用 RestAssure 执行后期操作,并且在标头中我传递 Content-Length 的位置如下:

queryParam().header("Content-Length", "value_Of_It")

因此,我遇到了以下错误:

Content-Length 标头已存在”

原因:org.apache.http.ProtocolException:Content-Length 标头已存在

如果有人能帮我解决这个问题那就太好了。

下面是默认情况下在邮递员中添加的标头,因此在“放心”中自动化时,我需要传递它们,如果没有,那么如何解决收到 400 Bad request 的问题? -

post automation rest-assured
2个回答
0
投票

Rest-Assured 在执行 POST 请求时自动将

Content-Length
添加到标头。我已经使用 Postman 代理进行了测试,以查看放心请求中的所有信息。

代码:

given().proxy(5555)
       .body("test")
       .post("http://postman-echo.com/post");

如果我添加另一个

Content-Length
,我将得到与你相同的错误。


0
投票

我遇到了同样的问题,我设法通过将内容类型从 JSON 更改为 TEXT 来解决此问题(尽管我已经为我的正文创建了一个 JSONObject)。

示例(无效的):

JSONObject payload = (my payload)
RestAssured.baseURI = "https://example.api.com";
            Response response = RestAssured.given()
                .contentType(ContentType.JSON)
                .header("Host", "example.api.com"")
                .body(payload)              
.post("https://example.api.com/endpoint");

由此我得到了内容长度错误。

但是当我将 contentType 更改为

ContentType.TEXT
并将主体的有效负载转换为字符串,
payload.toString
一切都开始正常工作。

我猜这不是一个通用的解决方案,但它可能会帮助有类似问题的人。

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