Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add headers to a httprequestmessage

Tags:

c#

I would like to add headers to a HttpRequestMessage. Problem is HttpRequestHeaders has no constructor, HttpHeaders is abstract, ... . Is there any way we can use these classes somewhere in our code ( I could use a List<Tuple<string, List<string>> but does not look nice at all)

like image 687
Dave Avatar asked Dec 13 '12 10:12

Dave


People also ask

How do I add a header to my HTTP request?

To add custom headers to an HTTP request object, use the AddHeader() method. You can use this method multiple times to add multiple headers. For example: oRequest = RequestBuilder:Build('GET', oURI) :AddHeader('MyCustomHeaderName','MyCustomHeaderValue') :AddHeader('MySecondHeader','MySecondHeaderValue') :Request.

How do you add a header to an HTTP request in Java?

Instead of setting the Header on each and every request, we can also configure it as a default header on the Client itself: Header header = new BasicHeader(HttpHeaders. CONTENT_TYPE, "application/json"); List<Header> headers = Lists. newArrayList(header); HttpClient client = HttpClients.


1 Answers

An HttpRequestMessage already has an instance of HttpRequestHeaders, and you can not change it. You can add a header like this:

message.Headers.Add("X-Hello", "world"); 
like image 107
Sjoerd Avatar answered Sep 22 '22 04:09

Sjoerd