Search code examples
flutterhttpdartinfluxdb

How can I use "application/vnd.flux" as content-type to do a POST request in dart?


I want to make a query to influxDB from a flutter app and I'm using http package, but I don't mind changing it. This is my current code (this request works well from terminal):

Response response = await post(
  URL,
  headers: {
    "Authorization": "Token $TOKEN",
    "Accept": "application/csv",
    "Content-type": "application/vnd.flux",
  },
  body: {
    'data': 'from(bucket: "$BUCKET")'
            '|> range(start: -48h)'
            '|> filter(fn: (r) => r._measurement == "location")'
  },
);

But I get this error:

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Bad state: Cannot set the body fields of a Request with content-type "application/vnd.flux".

How can I use application/vnd.flux as Content-type to do a POST request from here?


Solution

  • I found the error. If someone has the same problem, you should write the query raw in the body (without the parameter data):

    Response response = await post(
      URL,
      headers: {
        "Authorization": "Token $TOKEN",
        "Accept": "application/csv",
        "Content-type": "application/vnd.flux",
      },
      body: {
        'from(bucket: "$BUCKET")'
          '|> range(start: -48h)'
          '|> filter(fn: (r) => r._measurement == "location")'
      },
    );