locked
Adding headers to httpclient request RRS feed

  • Question

  • User-1165400470 posted

    Hi

    How do I add following headers to an httpclient request?

    contentType: "application/json; charset=utf-8",
    dataType: "json",
    headers:
                {
                        "Authorization": "Bearer " + apiToken
                }

    I have searched for it and there is a lot of information available but it is confusing as to which headings go where. 

    Thanks

    Regards

    Wednesday, August 19, 2020 3:26 PM

Answers

  • User-474980206 posted

    first nowadays its recommended to use a single HttpClient(), not create and dispose. as you want to set the token on each request, you use HttpRequestMessage:

    // can use injection, httpclient factory or
    static private HttpClient _httpClient = new HttpClient();
    
    
    ...
    
    async TOut AjaxPost<TOut>(string url, object data, string token)
    {  
    
       var requestMessage = new HttpRequestMessage
       {
           Method = HttpMethod.Post,
           Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"),
           RequestUri = new Uri(url)
       };
    
       requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
       var response = await _httpClient.SendAsync(requestMessage);
       
       if(response.IsSuccessStatusCode)
       {
              return await response.Content.ReadAsAsync<TOut>();
       }
       
       // handle error
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 19, 2020 6:53 PM

All replies

  • User753101303 posted

    Hi,

    Seems you are trying to use https://api.jquery.com/jquery.ajax/ ?

    Wednesday, August 19, 2020 3:36 PM
  • User-1165400470 posted

    Hi

    I am trying to convert from that to httpclient and I am new to both. My background is Winform.

    Thanks

    Regards

    Wednesday, August 19, 2020 3:43 PM
  • User-474980206 posted

    first nowadays its recommended to use a single HttpClient(), not create and dispose. as you want to set the token on each request, you use HttpRequestMessage:

    // can use injection, httpclient factory or
    static private HttpClient _httpClient = new HttpClient();
    
    
    ...
    
    async TOut AjaxPost<TOut>(string url, object data, string token)
    {  
    
       var requestMessage = new HttpRequestMessage
       {
           Method = HttpMethod.Post,
           Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"),
           RequestUri = new Uri(url)
       };
    
       requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
       var response = await _httpClient.SendAsync(requestMessage);
       
       if(response.IsSuccessStatusCode)
       {
              return await response.Content.ReadAsAsync<TOut>();
       }
       
       // handle error
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 19, 2020 6:53 PM