Axios is not defined

Has anyone managed to run axios.get / axios.post from a workflow?
We already included axios 1.7.6 into the libraries and use the request in a code block like this:

async function fetchFromJsonPlaceholder() {
  const response = await axios('https://jsonplaceholder.typicode.com/posts');
  return response.json();
}

return fetchFromJsonPlaceholder();

Hovering over "axios" shows a notification: 'axios' is not defined

It gives an error: {"data":null,"error":"ReferenceError: axios is not defined (line 3)"}

You may need to require it in the JS configuration.
As a workaround, why not use an API resource step? This functionality is built-in.

hi @hansontools,
we forgot to mention, that we have already added axios to the configuration like this:

image

We can not use the API resource step, because we try to do a polling on the target URL which is not possible with the API resource.

Therefore, we are trying to implement a solution provided by @Darren from this thread: Repeat API request if defined condition is not met - #2 by Darren

Its a loop inside a JS component.

Try require in the code block…

const axios = require('axios');

async function fetchFromJsonPlaceholder() {
  const response = await axios('https://jsonplaceholder.typicode.com/posts');
  return response.json();
}

return fetchFromJsonPlaceholder();

If i run the component alone, then this is the result:
image

Returning response by itself doesn't work in workflows, as it's probably way too dense for Retool workflow steps to work with: Response Schema | Axios Docs

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  // As of HTTP/2 status text is blank or unsupported.
  // (HTTP/2 RFC: https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2.4)
  statusText: 'OK',

  // `headers` the HTTP headers that the server responded with
  // All header names are lower cased and can be accessed using the bracket notation.
  // Example: `response.headers['content-type']`
  headers: {},

  // `config` is the config that was provided to `axios` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance in the browser
  request: {}
}
2 Likes

That worked, thanks a lot!