Difference between HttpRequestMessage.Headers and HttpRequestMessage.Content.Headers

Viewed 166

Could anyone please explain the difference between HttpRequestMessage.Headers and HttpRequestMessage.Content.Headers.

The documentation for HttpRequestMessage.Headers says Gets the collection of HTTP request headers.

And for HttpRequestMessage.Content.Headers - The content headers as defined in RFC 2616.

This doesn't help me a lot. What would be practical differences and which should be used?

1 Answers

We can see some properties defined in RFC 2616, searching for Header Field Definitions in the file, then we can see them should have a mapping with the properties of HttpRequestMessage.Content.Headers, they are introduced in this section.

It is literal meaning. And in my opinion, I think the differences are, e.g. when using jquery ajax in javascript, we adding all the request headers in the headers object, like

$.ajax({
        url: "url",
        type: 'post',
        headers: {
            Authorization: "Bearer token"
        },
        success: function(data) {alert(data);}
    })

But when we using HttpClientFactory in asp.net core, we have to set different http headers to different properties, so these are what I think as the differences. And these are the properties defined in HttpRequestMessage.Headers. You can see that they provides different properties.

enter image description here

Related