Help with http.post

I have a flux script like such:

import "http"
import "json"
data = from(bucket: "canopy")
    |> range(start: -10s)
    |> filter(fn: (r) => r["_measurement"] == "amqp_consumer")
    |> filter(fn: (r) => r["host"] == "jetson1")
    |> filter(fn: (r) => r["_field"] == "class_id")
    |> map(fn: (r) => ({r with _value: 1.0 - r._value}))
    |> mean()

http.post(url: "http://webapp:5000/allenbradley", headers: {}, data: json.encode(v: data[0]))

When I run this is receive an error saying “panic: length of stream not supported”.

Is there something wrong with how I am using the http.post function?

Hello @mattcarp12,
I don’t believe the json.encode() is throwing that error. That function can’t automatically encode an entire annotated csv output.

In the example from the docs (http.post() function | InfluxDB OSS 2.0 Documentation) findColumn is used to extract an array from the annotated csv output of the flux query. An item from this array is then being passed to the json.encode() function. Are you. looking to post one column? If so, try following this approach.

lastReported =
  from(bucket: "example-bucket")
    |> range(start: -1m)
    |> filter(fn: (r) => r._measurement == "statuses")
    |> last()
    |> findColumn(fn: (key) => true, column: "_level")

http.post(
  url: "http://myawsomeurl.com/api/notify",
  headers: {
    Authorization: "Bearer mySuPerSecRetTokEn",
    "Content-type": "application/json"
  },
  data: json.encode(v: lastReported[0])
)

If not, can you share your output data (annotated csv) and share what you expect the results of your http.post() to look like?

Thank you

1 Like

Hello @Anaisdg , thank you for your detailed reply.

Yes, I actually fixed my issue by using the findRecord() at the end of the query, and passing that into json.encode().

Just for my understanding, the mean() function return a table object, which even though has only a single record, that record must be selected from the table, as the json.encode() function does not accept tables, only record or other primitive type. Is this correct?

Thank you.