api call with https

Post Reply
User avatar
foxpalace
Member
Posts: 26
Joined: Fri Jan 14, 2011 12:25 pm
Location: Germany

api call with https

Post by foxpalace » Sat Jan 16, 2021 10:00 am

Hello,
whats wrong with this script, when I execute the Script with nodjs on Terminal it works, when I transpile it, the api-call will never run in Switch????

Code: Select all

const https = import('https');

async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {

    let datei = job.getName();
    let zielpfad = await flowElement.getPropertyStringValue("ZielPfad");
    var splittArray = datei.split("_");
    var jahrArray = splittArray[3].split(".");
    var jahr = 20 + jahrArray[0];
    var suffix = jahrArray[1];
    var objekt = splittArray[0];
    var kw = splittArray[2];

    await job.log(LogLevel.Info, "Jahr: " + jahr);
    await job.log(LogLevel.Info, "Suffix: " + suffix);
    await job.log(LogLevel.Info, "Objekt: " + objekt);
    await job.log(LogLevel.Info, "KW: " + kw);
    await job.log(LogLevel.Info, "ZielPfad: " + zielpfad);

    var url = 'https://url.com/api/v1/titles/' + objekt;
    await job.log(LogLevel.Info, "URL: " + url);

    (await https).get(url, (res) => {
        let body = "";

        res.on('data', (chunk) => {
            body += chunk;
        });

        res.on("end", () => {
            try {
                let json = JSON.parse(body);
                var verladsid = json.pub_publisher_id;
            } catch (error) {
                job.log(LogLevel.Info, "BLA");
            };
        });
    }).on("error", (error) => {
        job.log(LogLevel.Info, "BLA");
    });
}


When I try
https.get ... whitout await Visual Studio marks it as error

Regards Michael

freddyp
Advanced member
Posts: 726
Joined: Thu Feb 09, 2012 3:53 pm

Re: api call with https

Post by freddyp » Mon Jan 18, 2021 9:02 am

Same remark as for this post (viewtopic.php?f=26&t=3849): use Promises!

A word of advice for working with HTTP calls: use the axios package. It is easy to use and highly configurable.

Code: Select all

const axios = require("axios"); //JavaScript
import axios from "axios";  //TypeScript

...

    try {
      let result = await axios({method:"get", url:"https://url.com/api/v1/titles/" + objekt});
      //do something with the result
    } catch(error) {
      //do something with the error
    }

For more options and examples, see the axios documentation. When using the examples you find, always make sure not to use the .then callback functions. Remove them and await the axios call. It is as simple as that.

User avatar
foxpalace
Member
Posts: 26
Joined: Fri Jan 14, 2011 12:25 pm
Location: Germany

Re: api call with https

Post by foxpalace » Mon Jan 18, 2021 12:52 pm

Hi Freddy,
you made my day :)
I have to learn, to learn, to learn ...

Greetings Michael

borisCM
Member
Posts: 32
Joined: Mon May 06, 2019 12:23 pm
Location: Netherlands

Re: api call with https

Post by borisCM » Fri May 13, 2022 12:27 pm

Hi,

anyone has an example of how to use the axios interceptor to handle error responses?
https://axios-http.com/docs/interceptors

i actually just want to get the body of the response, no matter if it is a succes or a error.

freddyp
Advanced member
Posts: 726
Joined: Thu Feb 09, 2012 3:53 pm

Re: api call with https

Post by freddyp » Fri May 13, 2022 3:51 pm

The interceptors will not provide any added value. If you are only interested in the body of the return regardless of success or error then this is where you will find it:

Code: Select all

let body;
try {
    let result = await axios({method:"get", url:"https://url.com/api/v1/titles/" + objekt});
    body = result.data;
} catch(error) {
    body = error.response.data;
}
//do something with the body

Post Reply