0

I'm working in a code developed by someone else and I found this lines

HttpResponseMessage response = await Client.PostAsJsonAsync($"User", user);

coming from System.Net.Http.HttpClientExtensions

The code also use many similar methods likes:

Task<HttpResponseMessage> GetAsync(string requestUri);
Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content);
Task<HttpResponseMessage> PutAsync(string requestUri, HttpContent content);
Task<HttpResponseMessage> DeleteAsync(string requestUri);

coming from System.Net.Http.HttpClient

But I also need to send value in header (for versioning) so I modified my GetAsync by

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, $"User/{name}");
request.Headers.Add("ContenT-Type", "application/json;v=2.0");
HttpResponseMessage response = await Client.SendAsync(request);

I don't know if this is correct. Is there a way I can continue to work with get, post, update, postAsJson by setting my header or I'm force to use this generic send version?

Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
  • I think you are looking for [Client.DefaultRequestHeaders](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultrequestheaders?view=netframework-4.7.2) - set it once for all subsequent requests – vasily.sib Apr 15 '19 at 03:25
  • I cannot set the "Content-Type" in Client.DefaultRequestHeaders. So no. Already tried. Especially my Content-Type is not standard. Let check this https://stackoverflow.com/q/55644730/196526 – Bastien Vandamme Apr 15 '19 at 03:30
  • Oh, I see now. Then you may found [this link](https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request) useful. – vasily.sib Apr 15 '19 at 03:55

0 Answers0