[✅ Solution] Http.get is not providing or ignoring options, agent string and timeout?

I run into a problem with http.get, as a particular server seems to timeout and not respond with the user agent.

Seems also like I can’t set a timer for the request or change the agent string?
Is this true?

1 Like

Bump… is there a working way to set a timeout for an http request? Otherwise it just breaks my automation.

Hi @dirk_s,

thanks for sharing your issue! Currently, the built in Tape HTTP client exposed in workflow automations does not have a timeout setting. Feel free to create a dedicated feature request for that missing functionality, it might be easy to implement for us in the future.

Meanwhile, you could leverage async JS and try something like this:

/** Your timeout in milliseconds */
const TIMEOUT_MS = 5_000;

// launch a delayed promise and fail if reached without the http call finishing first
let success = false;
delay(TIMEOUT_MS).then(() => {
    if(!success) throw Error('Timeout, sorry')
});

// ... perform your request:
const result1 = await http.get("http://google.com");
console.log('Request finished without timeout.')
success = true;

Together with the “Continue on error” option that might help with your use case:

Let us know!
Good luck
Tim

1 Like

Thanks Tim for this response - I will try it and give feedback!

2 Likes

Thanks for this suggestion. It worked!

2 Likes

Hi,
Just in order to help and as a warning for future readers, http.get is not handling url redirects by default.

Thanks.
Best,
R.J.

1 Like

Thats the complete part of the code, that handles timeout and catches the response:

// Timeout value in milliseconds
const TIMEOUT_MS = 5000;

// Timeout mechanism: Promise that rejects after TIMEOUT_MS
const timeoutPromise = delay(TIMEOUT_MS).then(() => {
  if (!success) {
    console.error(`[${new Date().toISOString()}] Error: Timeout reached.`);
    throw new Error("Timeout reached, request aborted.");
  }
});

let httpResult = null;
let success = false;

try {
  // Perform the HTTP request
  const requestPromise = http.get(normalizedUrl, {
    followRedirects: false,
    headers: {
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
    }
  });

  // Wait for either the HTTP response or the timeout
  httpResult = await Promise.race([requestPromise, timeoutPromise]);
  success = true;

  // Handle the response
  const status = httpResult?.status || 200;
  console.log(`[${new Date().toISOString()}] HTTP Status: ${status}`);

  var_response = status;
  var_redirect = null;

  if (status === 302 && httpResult.headers?.location) {
    const urlObj = new URL(httpResult.headers.location);
    urlObj.search = ""; // Remove query parameters
    var_redirect = urlObj.href;
  }

} catch (error) {
  console.error(`[${new Date().toISOString()}] Request failed: ${error.message}`);
  
  var_response = error.message.includes("Timeout") ? "timeout_error" : "http_error";
  var_redirect = null;
}

1 Like