400 Bad Request: Http Request Fails

Micah Holmes 121 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?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,288 questions
Remote Desktop
Remote Desktop
A Microsoft app that connects remotely to computers and to virtual apps and desktops.
4,260 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 48,826 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