Search code examples
c#asp.netasp.net-mvcpostinfluxdb

Bad request during POST request to influxDB from asp.net


I'm trying to send a POST request to my local influxDB. I'm getting StatusCode: 400, ReasonPhrase: 'Bad Request'"

 HttpClient client = new HttpClient();
 HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost:8086/write?db=mydb");
 requestMessage.Content = new StringContent("cpu_load_short, host = server01, region = us - 345345 value = 0.34564 345345");
 HttpResponseMessage response = client.SendAsync(requestMessage).GetAwaiter().GetResult();

In my Postman I put "http://localhost:8086/write?db=mydb" and "cpu_load_short,host=server01,region=us-345345 value=0.34564 345345" in my body(raw) and it works


Solution

  • try setting the content type to something like

    request.ContentType = "application/x-www-form-urlencoded"; 
    

    or if you know and are sure about the encoding type as well then use this

    requestMessage.Content = new StringContent("cpu_load_short, host = server01, region = us - 345345 value = 0.34564 345345", Encoding.UTF8, "application/x-www-form-urlencoded"); // or whatever is the encoding type
    

    The ContentType property contains the media type of the request