How to write data in Influxdb using c#


I want to send data to Influxdb rest API v2. However, I get a 400 error and cannot send data. Because unity is being used, the corresponding influxDB.client library conflicts with assembly and cannot be used. How do I send data to influxDb 2.0 and write it?

this is my code

const string url = "http://localhost:8086/api/v2/write org=DT_navigator&bucket=test_bk&precision=s";

     async System.Threading.Tasks.Task WriteDataAsync()
      var json = JsonConvert.SerializeObject("mem,host=host1 used_percent=23");
      var data = new StringContent(json, Encoding.UTF8, "application/json");

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("Authorization", "Token " + token);

    var response = await client.PostAsync(url, data);

    string result = response.Content.ReadAsStringAsync().Result;

and error message {“code”:“invalid”,“message”:“unable to parse ‘“mem,host=host1 used_percent=23”’: invalid number”}

Since it looks like your data is already in Influx Line Protocol, you shouldn’t need to convert it to JSON before sending it to the write API

1 Like