Need help with JS script and a curl command

Hello,

I try since a lot of time to translate this curl command into a JS rule code

Here is the curl command

curl -X GET "http://myapi:8080/api/auth" \
 -H "accept: application/json" 

How does this curl command look like in a JS rule syntax?

here is my try

const http = require('http')

const options = {
    method: 'GET',
    hostname: 'http://myapi:8080/api',
    port: null,
    path: '/auth',
    headers: {accept: application/json},
}

const req = http.request(options, function (res) {
    const chunks = []

    res.on('data', function (chunk) {
        chunks.push(chunk)
    })

    res.on('end', function () {
        const body = Buffer.concat(chunks)
        console.log(body.toString())
    })
})

req.end()

getting this error

Failed to execute rule test: TypeError: Cannot load CommonJS module

Thanks for the hint :slight_smile:

GraalVM is a Node.js like environment but it is not 100% a Node.js environment. Not everything that exists in Node.js is available, notably the http.

But that’s what the sendHttp Rule Actions are for.

Note, if this is a UI Script Action, you cannot have const and let in the top level context of the action. The script gets reused from one run to the next and as a result the second time the rule runs it will complain that it “cannot redefine const options” (for example).

var results = actions.sendHttpGetRequest('http://myapi:8080/api/auth', {"accept": "application/json"}, 5000);
console.log(results);

I’m not 100% I defined the header’s Map right but you get the point.

You also have the option to use the HTTP Binding instead of a rule.

getting this error now

Failed to execute action: 1(Error: Failed to execute rule test: TypeError: (intermediate value).sendHttpGetRequest is not a function: TypeError: (intermediate value).sendHttpGetRequest is not a function

Typo on my part. See the JS Scripting docs for details.

... actions.HTTP.sendHttpGetRequest(...