400 Bad Request: Http Request Fails

Micah Holmes 126 Reputation points
2022-03-15T18:59:36.783+00:00

Not sure why this request fails on my client but works when I test using Postman:

code:

httpClient.BaseAddress = new Uri(endpointURL);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
httpClient.DefaultRequestHeaders.Add("OfficeCode", Settings.Default.officeCode);
httpClient.DefaultRequestHeaders.Add("ContactTypeName", Settings.Default.ContactTypeName);

Keep saying I'm sending a bad request but when I test using postman, it works. Am I missing something simple here?

Windows for business Windows Client for IT Pros User experience Remote desktop services and terminal services
Developer technologies ASP.NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-03-15T20:07:05.74+00:00

    That isn't how you use HttpClient and therefore you could be running into issues with the client itself. HttpClient has a couple of properties on it including BaseAddress and DefaultRequestHeaders. However these are designed for all calls to that client and not per-request. If you attempt to use these properties after the client has been used the first time you are going to get into trouble. So, for example, the following call I would not expect to work.

       var client = new HttpClient() {  
          BaseAddress = new Uri("...")  
       };  
         
       client.GetAsync(...);  
         
       client.BaseAddress = new Uri("...");  
       client.DefaultRequestHeaders.Add(...);  
    

    To properly use HttpClient you need to create an instance and set the base address along with any request headers that should be sent with all requests (and therefore are unchanged). This is normally very high level stuff like using JSON for everything.

       public class MyService  
       {  
          public MyService ( string url )  
          {  
             _client = new HttpClient() {  
                BaseAddress = new Uri(url)   //Ensure the URL ends with a slash...  
             };  
         
             _client.DefaultRequestHeaders.Add(...);  
          }  
         
          public Task DoSomething ()  
          {  
             return _client.GetAsync(...);  
          }  
         
          //Ignoring HttpClient lifetime for this discussion  
          private readonly HttpClient _client;  
       }  
    

    If you need to manipulate the headers on a per-request basis then the correct approach is to create an HttpRequestMessage and use it instead.

       public Task DoSomething ( int someValue )  
       {  
          //Second parameter is the URL after the base address...  
          using (var msg = new HttpRequestMessage(HttpMethod.Get, ""))  
          {  
             //Add per-request headers  
             msg.Headers.Add(...);  
         
             return _client.SendAsync(msg);  
          };  
       }  
    

    Beyond that we can only assume you're passing the right HTTP headers. Showing us the Postman info would be good.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.