Struggling with http.post()

Hello,

I am trying to send an email using http.post and sendinblue. The task executes successfully but nothing happens. Here is the script.


I have used the same headers and data to send an email using Matlab (webwrite as well as RequestMessage) so the sendinblue api seems to work well. How can I get the response of the http.post command and write it to a log file to start debugging?

Thanks,

SpeedY

Hi SpeedY,

Your script looks good. We have learned a few tricks to make debugging http requests earier. Let me share them here.

As you have probably noticed the query needs to produce some data in order for it to run without error. I am guessing that is why you are reading data from the TestBucket.

Another way to do this is to put the result of the http.post into a table and return that.

For example:

import "http"
import "json"
import "array"

SENDINBLUE_APIKEY = secrets.get(key: "SENDINBLUE_APIKEY") // aside I recommend you use the secrets feature so the key doesn't have to be in plain text

statusCode = http.post( .... ) // same as you it above

array.from(rows:[{statusCode: statusCode})

The above should work well and report the status code of the HTTP request. However we just released a new http package called requests that can make this easier.

import "experimental/http/requests"
import "json"
import "array"

SENDINBLUE_APIKEY = secrets.get(key: "SENDINBLUE_APIKEY")

response = requests.post(
    url: "https://api.sendinblue.com/v3/smtp/email",
    // Notice the new syntax here for the headers
    headers: ["Accept": "application/json", "Content-Type": "application/json", "api-key": SENDINBLUE_APIKEY],
    data: json.encode(
        v: {
            to: [{email: "YYY@hotmail.com"}],
            sender: [{email: "XXX@hotmail.com"}],
            subject: "InfluxDB email test",
            content: [{type:"text/plain", value: "Hello!"}],
         }
     )
)

// The new request package returns the entire response not just the status code.
// Do the same thing and put the status code and response body into a table to return
array.from(rows:[{
    body: string(v: response.body),
    statusCode: response.statusCode,
}])

You can find the complete requests package docs here Flux experimental http requests package | Flux 0.x Documentation

NOTE: We are currently making the last step easier by adding a peek method to the requests package that does the array.from step for you. If you are curious see the PR here https://github.com/influxdata/flux/pull/4446 You will be able to use it like requests.peek(response: response) which is much easier to remember than the array.from call.

1 Like

Hello nathaniel,

Thanks for the info on the new experimental package. I always get an error (Failed to update task: failed to update task: option not found) after adding the line import “experimental/http/requests” Are you sure that this package is available? I am using https://eu-central-1-1.aws.cloud2.influxdata.com. The error is “invalid import path experimental/http/requests”. BTW, using the first version the http response status code is 400 (bad request).

Cheers,

SpeedY

Hello again,

Apparently the bad request was triggered by the “content” of the data . The following version works, the http response status code is 201 and the email was sent by sendinblue.

Cheers,

SpeedY