n8n HTTP Request Node Invalid JSON Response Error

Step by Step Guide to solve n8n HTTP Request Node Invalid JSON Response Error

 


 

Who this is for: n8n workflow creators who receive “Invalid JSON response” errors from the HTTP Request node and need a reliable, production‑ready fix. We cover this in detail in the n8n Node Specific Errors Guide.

Quick Diagnosis: Fix the error in seconds

  1. Verify the endpoint really returns JSON (browser or curl).
  2. Temporarily set Response FormatString in the HTTP Request node.
  3. Inspect the raw payload for HTML, extra characters, or a BOM.
  4. If the payload is valid JSON, switch Response Format back to JSON.
  5. If it isn’t JSON, add a Set → JSON Parse or Function node to transform the data.

*Copy the checklist at the end of the article to your workflow and the error disappears.*

1. Why the “Invalid JSON response” error appears

If you encounter any n8n http request node error 401 resolve them before continuing with the setup.

Root causes and how n8n detects them:

Root Cause Detection
Non‑JSON payload (HTML error page, plain text, CSV) `JSON.parse()` throws a SyntaxError.
BOM / invisible characters before { or [ Parsing fails despite JSON‑like appearance.
Wrong Content‑Type header (e.g., text/html) Node defaults to “String” and later tries to parse.
Payload > 10 MB (default limit) n8n aborts parsing to protect memory.
Server‑side compression not decoded (gzip) Binary gibberish is fed to JSON.parse().

EEFA note: Disabling strict JSON validation (Continue On Fail) only masks the problem and is not a production‑grade fix.

2. Step‑by‑step debugging workflow

If you encounter any n8n http request node timeout resolve them before continuing with the setup.

2.1 Capture the raw response

Purpose: See exactly what the API returns before n8n attempts to parse it.

  1. Open the **HTTP Request** node.
  2. Set **Response Format** → **String** (temporary).
  3. Run the node.
  4. In **Execution → Output**, copy the **Body** field.

Tip: If the body starts with <!DOCTYPE html> or contains <html>, you’re looking at an HTML error page (e.g., 404, 500).

2.2 Verify JSON validity with curl

curl -s -H "Accept: application/json" "https://api.example.com/data" |
jq . > /dev/null && echo "Valid JSON" || echo "Invalid JSON"

Result:

  • Valid JSON → the endpoint returns well‑formed JSON.
  • Invalid JSON → inspect the raw output for the issue.

2.3 Common fixes

Issue Fix
HTML error page Check URL, auth, or query parameters. Use an **If** node to branch on non‑2xx status codes.
BOM / whitespace Add a **Function** node to strip it (see snippet below).
Wrong Content‑Type Set header Accept: application/json and keep **Response Format** = **JSON**.
Compressed payload Enable **Decompress response** or add header Accept‑Encoding: gzip, deflate.
Payload too large Increase **Maximum response size** (monitor memory usage).

Stripping BOM / whitespace

// Function node: clean the raw string before parsing
const raw = $json["body"];
const cleaned = raw.replace(/^\uFEFF/, "").trim();
return { json: JSON.parse(cleaned) };

2.4 Re‑enable proper JSON parsing

  1. After confirming the payload is clean JSON, switch **Response Format** back to **JSON**.
  2. Run the node again – the error should disappear.

If the API cannot return JSON (e.g., CSV), keep **Response Format** as **String** and add a **Function** or **CSV Parse** node to convert the data for downstream steps.

3. Real‑world example: Fixing a mis‑configured third‑party API

3.1 Problem description

A workflow that pulls product data from https://api.shopify.com/v1/products.json fails with:

Error: Invalid JSON response

The captured raw payload (with “Response Format: String”) begins with:

<!DOCTYPE html>
<html>
<head><title>401 Unauthorized</title></head>
...

3.2 Solution steps

  1. Add an “If” node to test {{$node["HTTP Request"].statusCode}} for 200.
  2. Insert a “Set” node before the request to include the required header.

Adding the authentication header

{
  "parameters": {
    "url": "https://api.shopify.com/v1/products.json",
    "method": "GET",
    "responseFormat": "JSON",
    "headerParametersUi": {
      "parameter": [
        {
          "name": "X-Shopify-Access-Token",
          "value": "{{$env.SHOPIFY_TOKEN}}"
        }
      ]
    }
  },
  "name": "HTTP Request",
  "type": "n8n-nodes-base.httpRequest",
  "typeVersion": 2,
  "position": [400, 300]
}

Re‑run the workflow – the node now returns proper JSON and the error disappears.

EEFA warning: Storing API keys in plain text inside a workflow is insecure. Use **n8n credentials** or **environment variables** ({{$env.VAR_NAME}}).

4. Checklist – Prevent “Invalid JSON response” before it happens

If you encounter any n8n webhook node missing signature resolve them before continuing.

  • [ ] Validate endpoint with a tool (Postman, curl) – ensure it returns JSON.
  • [ ] Set proper Accept header (application/json).
  • [ ] Confirm correct HTTP method (GET vs POST).
  • [ ] Verify authentication (API keys, OAuth tokens) – 401/403 often return HTML.
  • [ ] Enable “Decompress response” if the server sends gzip.
  • [ ] Use **Response Format** = **JSON** only after confirming payload is JSON.
  • [ ] Add error‑branching (If node) for non‑2xx status codes.
  • [ ] Log raw payload (String mode) during development.

5. Advanced – Handling APIs that intermittently return malformed JSON

Some legacy APIs prepend an anti‑XSS prefix like )]}', before the JSON:

)]}',{"data":{...}}

Fix with a Function node

// Remove the prefix if present
const raw = $json["body"];
const cleaned = raw.replace(/^\\)\\]\\}',/, "");
return { json: JSON.parse(cleaned) };

Place this **Function** node **after** the HTTP Request (set to **String**) and **before** any downstream node that expects JSON.

EEFA tip: Wrap the parsing in a try / catch block and route failures to a dedicated **Error Handling** workflow to avoid silent data loss.

Conclusion

The “Invalid JSON response” error is almost always traceable to a mismatch between what the API actually returns and what n8n expects. By capturing the raw payload, verifying JSON validity, correcting headers or authentication, and handling edge cases (BOM, compression, size limits) with small, focused nodes, you can eliminate the error and keep your workflows robust in production. Use the checklist to pre‑empt issues, and remember to keep secrets out of the workflow definition for security.

Leave a Comment

Your email address will not be published. Required fields are marked *