C#C
C#4y ago
sonodan.

Http 400 Bad Request Response [Answered]

POST controller in WebAPI.

[Route("api/[controller]")]
  [ApiController]
  public class EventsController : ControllerBase
  {
    [HttpPost]
    public async Task UpdateContacts(List<EventModel> events)
    {
      // Do things here

    }
  }


Service from the client that calls the Web API:

    public async Task UpdateEvents(int userId, List<EventModel> events)
    {
      var contractResolver = new DefaultContractResolver()
      {
        NamingStrategy = new CamelCaseNamingStrategy()
      };

      var json = JsonConvert.SerializeObject(events, new JsonSerializerSettings
      {
        ContractResolver = contractResolver,
        Formatting = Formatting.Indented
      });

      _httpClient.DefaultRequestHeaders.Clear();
      _httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

      var result = await _httpClient.PostAsJsonAsync($"api/events", json);
    }


I've tried many things, including serializing the object to Json using Newtonsoft and just passing the object in for the HttpClient to serialize.

I keep getting a 400 bad request response, and the content doesn't seem to provide any insight as to why that is. Note that I have provided the correct base URL to the httpClient as it works for my GET requests.
Was this page helpful?