Http request invalid URL error

Hey there,

having an issue with a http-request node and an invalid URL (Staffbase.com) error. Total newbie, so appreciate any help on this. https://www.google.com did work in the http request node.

{
  "errorMessage": "Invalid URL",
  "errorDetails": {
    "rawErrorMessage": [
      "Invalid URL"
    ],
    "httpCode": "ERR_INVALID_URL"
  },
  "n8nDetails": {
    "nodeName": "HTTP Request",
    "nodeType": "n8n-nodes-base.httpRequest",
    "nodeVersion": 4.2,
    "itemIndex": 0,
    "time": "6/27/2025, 1:45:52 PM",
    "n8nVersion": "1.99.1 (Cloud)",
    "binaryDataMode": "filesystem",
    "stackTrace": [
      "NodeApiError: Invalid URL",
      "    at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-nodes-base@file+packages+nodes-base_@[email protected]_asn1.js@5_12b981d6b49d407a163f4d5244314033/node_modules/n8n-nodes-base/nodes/HttpRequest/V3/HttpRequestV3.node.ts:780:15)",
      "    at processTicksAndRejections (node:internal/process/task_queues:105:5)",
      "    at WorkflowExecute.runNode (/usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-core@[email protected][email protected][email protected]_/node_modules/n8n-core/src/execution-engine/workflow-execute.ts:1187:9)",
      "    at /usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-core@[email protected][email protected][email protected]_/node_modules/n8n-core/src/execution-engine/workflow-execute.ts:1536:27",
      "    at /usr/local/lib/node_modules/n8n/node_modules/.pnpm/n8n-core@[email protected][email protected][email protected]_/node_modules/n8n-core/src/execution-engine/workflow-execute.ts:2100:11"
    ]
  }
}

Workflow:

Set node array:

Code 1 Node:

return items.map(item => {
  // Assuming the input to THIS node has 'url' at the top-level OR wherever it currently is.
  // We want to ensure the output ALWAYS has it under item.json.url
  let inputUrl = item.url || item.json.url; // Try to get it from top-level or json

  if (!item.json) { // Ensure item.json exists
    item.json = {};
  }

  // Explicitly assign it to item.json.url
  item.json.url = inputUrl; 
  item.json.name = item.name || item.json.name; // Also ensure name is in json for consistency

  // Remove the old top-level 'url' and 'name' if they exist to prevent conflicts
  delete item.url; 
  delete item.name;

  return item;
});

URL Sanitizer Node:

return items.map(item => {
  let originalUrl = item.json.url; // Access URL from the json object

  // *** NEW: Add a very explicit check and log here ***
  if (originalUrl === undefined || originalUrl === null) {
    console.error("DEBUG: originalUrl is UNDEFINED or NULL for item:", item);
    item.json.sanitizationError = "DEBUG: URL value missing from input item.";
    item.json.sanitizedUrl = null; // Ensure no invalid URL is passed
    return item;
  }
  if (typeof originalUrl !== 'string') {
      console.error("DEBUG: originalUrl is not a string for item:", item, "Type:", typeof originalUrl);
      item.json.sanitizationError = "DEBUG: URL value is not a string.";
      item.json.sanitizedUrl = null;
      return item;
  }
  // *** END NEW ***

  // Initialize cleanedUrl within the JSON object
  item.json.sanitizedUrl = originalUrl; // Default to original in case of error
  item.json.sanitizationError = null; // Clear previous error if any

  try {
    const urlObject = new URL(originalUrl); // This is where the error previously occurred
    
    item.json.sanitizedUrl = urlObject.protocol + '//' + urlObject.host + urlObject.pathname + urlObject.search + urlObject.hash;
    
    console.log(`Original URL: ${originalUrl}, Cleaned URL: ${item.json.sanitizedUrl}`);

  } catch (e) {
    console.error(`Error sanitizing URL '${originalUrl}': ${e.message}`);
    item.json.sanitizationError = `URL parsing error: ${e.message}`;
    item.json.sanitizedUrl = originalUrl; 
  }
  
  return item;
});

Information on your n8n setup

Node type: n8n-nodes-base.httpRequest
Node version: 4.2 (Latest)
n8n version: 1.99.1 (Cloud)
Device: MacOS 14.4.1

The only piece of information you forgot to mention is what the url ends up to be. How is http request node is configured? Try to embed the workflow instead of posting a screenshot, that would help a great deal.

It looks like your URL field value is not surrounded by curly braces, try
{{ $json.sanitizedUrl }}. And change the field to expression type.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.