Response Header Content-Length not available when call HEAD Method #400

Closed
opened 2025-12-29 08:27:26 +01:00 by adam · 9 comments
Owner

Originally created by @bangush on GitHub (Feb 7, 2022).

Originally assigned to: @StefH on GitHub.

Refer to the Previous Bug #136 and Bug #137. When call HEAD Method the Cotent-Length is not available.

const string responseBody = @"<?xml version=""1.0""?><empty/>";

wireMockServer
.Given
(Request
.Create()
.WithPath("/api")
.UsingPost()
)
.RespondWith(Response
.Create()
.WithStatusCode(HttpStatusCode.OK)
.WithHeader("content-type", "text/xml")
.WithHeader("content-length", responseBody.Length.ToString()) // this line not show during test.
.WithBody(responseBody)
);
Originally created by @bangush on GitHub (Feb 7, 2022). Originally assigned to: @StefH on GitHub. Refer to the Previous Bug #136 and Bug #137. When call HEAD Method the Cotent-Length is not available. ``` c# const string responseBody = @"<?xml version=""1.0""?><empty/>"; wireMockServer .Given (Request .Create() .WithPath("/api") .UsingPost() ) .RespondWith(Response .Create() .WithStatusCode(HttpStatusCode.OK) .WithHeader("content-type", "text/xml") .WithHeader("content-length", responseBody.Length.ToString()) // this line not show during test. .WithBody(responseBody) ); ```
adam added the feature label 2025-12-29 08:27:26 +01:00
adam closed this issue 2025-12-29 08:27:26 +01:00
Author
Owner

@bangush commented on GitHub (Feb 7, 2022):

Hint: I randomly check multiple versions, and up-to WireMock.Net Ver 1.0.4.19 work with header contents and return the values as expected. but after that the process is broken.

{
var headers = new Dictionary<string, string>
        {
            { "Accept", "text/html" }, // OK
            { "Connection", "keep-alive" }, // OK
            { "Content-Length", responseBody.Length.ToString() }, // OK
            { "Content-Type", "text/html; charset=utf-8" }, // OK
            { "Date", "Fri, 06 Mar 2019 17:36:11 GMT" }, // OK
            { "Expect", "100-continue" }, // OK
            { "Host", "developer.mozilla.org" }, // OK
            { "If-Modified-Since", "Wed, 21 Oct 2015 07:28:00 GMT" }, // OK
            { "Keep-Alive", "timeout=5, max=1000" }, // OK
            { "Range", "bytes=0-499, -500" }, // OK
            { "Referer", "developer.mozilla.org/en-US/docs/Web/JavaScript" }, // OK 
        **//  { "Transfer-Encoding", "compress" },  // 500 Internal Server Error** 
          { "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" }, // OK
            { "Proxy-Connection", "keep-alive" }, // OK
            { "WWW-Authenticate", "Basic YWxhZGRpbjpvcGVuc2VzYW1l" } // OK 
        };
...
...
 .WithHeaders(headers)
...
...
}
@bangush commented on GitHub (Feb 7, 2022): Hint: I randomly check multiple versions, and up-to WireMock.Net Ver 1.0.4.19 work with header contents and return the values as expected. but after that the process is broken. ``` c# { var headers = new Dictionary<string, string> { { "Accept", "text/html" }, // OK { "Connection", "keep-alive" }, // OK { "Content-Length", responseBody.Length.ToString() }, // OK { "Content-Type", "text/html; charset=utf-8" }, // OK { "Date", "Fri, 06 Mar 2019 17:36:11 GMT" }, // OK { "Expect", "100-continue" }, // OK { "Host", "developer.mozilla.org" }, // OK { "If-Modified-Since", "Wed, 21 Oct 2015 07:28:00 GMT" }, // OK { "Keep-Alive", "timeout=5, max=1000" }, // OK { "Range", "bytes=0-499, -500" }, // OK { "Referer", "developer.mozilla.org/en-US/docs/Web/JavaScript" }, // OK **// { "Transfer-Encoding", "compress" }, // 500 Internal Server Error** { "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0" }, // OK { "Proxy-Connection", "keep-alive" }, // OK { "WWW-Authenticate", "Basic YWxhZGRpbjpvcGVuc2VzYW1l" } // OK }; ... ... .WithHeaders(headers) ... ... } ```
Author
Owner

@StefH commented on GitHub (Feb 7, 2022):

@bangush
According to the specs, Head does not have a body.
So also not a content-length.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD

image

@StefH commented on GitHub (Feb 7, 2022): @bangush According to the specs, Head does not have a body. So also not a content-length. https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD ![image](https://user-images.githubusercontent.com/249938/152831601-b6d17260-6346-42a5-a0f0-e268642265d0.png)
Author
Owner

@textbook commented on GitHub (May 30, 2023):

As is visible in the quote:

For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.

Per RFC9110, although:

...the server MUST NOT send content in the response. ... The server SHOULD send the same header fields in response to a HEAD request as it would have sent if the request method had been GET.

So although a HEAD response is not required to have a Content-Length, it certainly (and despite the lack of actual content) can if that length is known.

@textbook commented on GitHub (May 30, 2023): As is visible in the quote: > For example, if a URL might produce a large download, a HEAD request could read its [Content-Length](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length) header to check the filesize without actually downloading the file. Per [RFC9110](https://www.rfc-editor.org/rfc/rfc9110.html#HEAD), although: > ...the server **MUST NOT** send content in the response. ... The server **SHOULD** send the same header fields in response to a HEAD request as it would have sent if the request method had been GET. So although a HEAD response is not _required_ to have a `Content-Length`, it certainly (and despite the lack of actual content) _can_ if that length is known.
Author
Owner

@maoso commented on GitHub (Jun 6, 2023):

Hi @StefH,

Is this issue going to be fixed? A head response returns the content-length of the file but WireMock not.

@maoso commented on GitHub (Jun 6, 2023): Hi @StefH, Is this issue going to be fixed? A head response returns the content-length of the file but WireMock not.
Author
Owner

@dianast31 commented on GitHub (Aug 13, 2024):

Hi @StefH,

Is this issue going to be fixed? I have the same problem as @maoso. Is there a workaround for this bug?

@dianast31 commented on GitHub (Aug 13, 2024): Hi @StefH, Is this issue going to be fixed? I have the same problem as @maoso. Is there a workaround for this bug?
Author
Owner

@StefH commented on GitHub (Aug 13, 2024):

I will have a look if it's possible to support content-lenght for head.

@StefH commented on GitHub (Aug 13, 2024): I will have a look if it's possible to support content-lenght for head.
Author
Owner

@StefH commented on GitHub (Aug 16, 2024):

https://github.com/WireMock-Net/WireMock.Net/pull/1158

@StefH commented on GitHub (Aug 16, 2024): https://github.com/WireMock-Net/WireMock.Net/pull/1158
Author
Owner

@dianast31 commented on GitHub (Aug 16, 2024):

#1158

Hi @StefH,

Thank you so much; we really need this feature ASAP. Could you please provide an ETA for the next release that will include this fix?

@dianast31 commented on GitHub (Aug 16, 2024): > #1158 Hi @StefH, Thank you so much; we really need this feature ASAP. Could you please provide an ETA for the next release that will include this fix?
Author
Owner

@StefH commented on GitHub (Aug 16, 2024):

Today

@StefH commented on GitHub (Aug 16, 2024): Today
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#400