[英]Netty Set Content-Length Automatically
I want Netty (Reactor Netty, to be specific) to set the Content-Length
header in my requests.我希望 Netty(具体来说是 Reactor Netty)在我的请求中设置
Content-Length
标头。 Currently, even when I send a request body in my request, the Content-Length
header is not set.目前,即使我在请求中发送请求正文,也未设置
Content-Length
标头。 Is it possible to configure Netty
to set this automatically (set it to 0
if there is no request body)?是否可以将
Netty
配置为自动设置(如果没有请求正文,则将其设置为0
)? Postman
does this. Postman
这样做。
Thanks!谢谢!
我可以为 netty 回答这个问题......在 netty 中,这不是自动完成的,唯一的方法是将其设置为您自己或添加一个ChannelOutboundHandler
根据它收到的FullHttpMessage
此操作。
It depends on the content that you want to send.这取决于您要发送的内容。 If it is of type
Mono
, then we will calculate the content length and send a FullHttpMessage
.如果它是
Mono
类型,那么我们将计算内容长度并发送一个FullHttpMessage
。 If it is of type Flux
, we will consider this as a chunked content and thus we will not calculate the content length.如果它是
Flux
类型,我们会将其视为分块内容,因此我们不会计算内容长度。 Here is an example with Mono
:这是
Mono
的示例:
public static void main(String[] args) {
String response =
HttpClient.create()
.wiretap(true)
.post()
.uri("https://postman-echo.com/post")
.send(Mono.just(Unpooled.wrappedBuffer("something".getBytes(Charset.defaultCharset()))))
.responseContent()
.aggregate()
.asString()
.block();
System.out.println(response);
}
In the logs you should be able to see this:在日志中,您应该能够看到:
17:01:46.813 [reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id: 0x668bd78f, L:/xxx:xxx - R:postman-echo.com/34.239.20.132:443] WRITE: 118B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 50 4f 53 54 20 2f 70 6f 73 74 20 48 54 54 50 2f |POST /post HTTP/|
|00000010| 31 2e 31 0d 0a 75 73 65 72 2d 61 67 65 6e 74 3a |1.1..user-agent:|
|00000020| 20 52 65 61 63 74 6f 72 4e 65 74 74 79 2f 64 65 | ReactorNetty/de|
|00000030| 76 0d 0a 68 6f 73 74 3a 20 70 6f 73 74 6d 61 6e |v..host: postman|
|00000040| 2d 65 63 68 6f 2e 63 6f 6d 0d 0a 61 63 63 65 70 |-echo.com..accep|
|00000050| 74 3a 20 2a 2f 2a 0d 0a 63 6f 6e 74 65 6e 74 2d |t: */*..content-|
|00000060| 6c 65 6e 67 74 68 3a 20 39 0d 0a 0d 0a 73 6f 6d |length: 9....som|
|00000070| 65 74 68 69 6e 67 |ething |
+--------+-------------------------------------------------+----------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.