I am trying to make an service that posts some data to an API Endpoint using C# HttpClient. The code is as follows.
public class HttpClientService : IHttpClientService
{
static HttpClient client = new HttpClient();
public HttpClientService()
{
client.BaseAddress = new Uri("http://xx.xx.xx.xx/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<Uri> MakeLogEntry(CnsLog log)
{
HttpResponseMessage response = await client.PostAsJsonAsync("api/logs", log);
return response.Headers.Location;
}
}
The problem is that the end point returns error 411 Length Required. I have found that this is because my request doesn't have the content-length header set, which I found to be true when I inspected the request using Fiddler.
I have tried to set the content length header on the client in the constructor but the code doesn't compile after that. I'm stuck and would appreciate any help. Thanks