Invalid JSON in HTTP Request node

Describe the problem/error/question

Hey n8n team, thanks in advance for your help,
I’m passing the data generated from Set Node to HTTP Request Node, but I got that error in the image below, I already tried set in the HTTP node in RAW mode with application/json as well as JSON mode with no luck

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

[
  {
    "Dte": {
      "datosGenerales": {
        "tipo": "\"FACT\"",
        "fechaHoraEmision": "\"2023-06-16T18:28:11\"",
        "codigoMoneda": "\"GTQ\""
      },
      "emisor": {
        "direccion": {
          "direccion": "\"Ciudad\"",
          "codigoPostal": "\"01001\"",
          "municipio": "\"\"",
          "departamento": "\"\"",
          "pais": "\"GT\""
        },
        "nitEmisor": "\"107902281\"",
        "nombreEmisor": "\"DUMMY\"",
        "codigoEstablecimiento": "\"0\"",
        "nombreComercial": "\"DUMMY\"",
        "correoEmisor": "\"[email protected]\"",
        "afiliacionIVA": "\"GEN\""
      },
      "receptor": {
        "direccion": {
          "direccion": "\"Ciudad  \"",
          "codigoPostal": "01001"
        }
      }
    }
  }
]

Information on your n8n setup

  • n8n version: 0.231.2
  • Database (default: SQLite): PostgreSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system:

Hi @rodmontgt, I am sorry to hear you’re having trouble. This error suggest that n8n can’t process the response data it receives from your API.

In a first step, you could try setting the response format (under Options → Add Option → Response) to Text like so:

image

This should prevent n8n from trying to parse the response as JSON data.

1 Like

Sadly I got this error:

It works with the same payload but on Postman, it returns a XML payload if everything is OK

Any ideas?

Hi @rodmontgt, I am afraid I can’t reproduce this. Fetching XML data should work fine in general, I tested this using the below workflow:

This is my result:

Is there any chance you can share an example using which your problem can be reproduced? You can for example use a service such as webhook.site to create a dummy API endpoint returning XML data with anything confidential redacted (click the Edit button to mock a response).

Hi again, thanks for your help,
I already tried putting the raw data in the JSON body of the HTTP request node, and it works like a charm, so the problem is with the data that I’m sending dynamically taken from the previous node.

I realized I’m sending data inside brackets, like an array, but I need to send the data without those objects.

I tried removing the brackets with the following ways but with no luck:

I tried this code outside n8n and is working but inside n8n it gives me an error:

var json = $input.all();
var parsedJson = JSON.parse(json);
var jsonString = JSON.stringify(parsedJson[0]);

return jsonString;

or with this:

// Remove outer brackets
const jsonString = JSON.stringify(jsonData[0]);

// Output data
return  jsonString;

This is the input data

[
  {
    "Dte": {
      "datosGenerales": {
        "tipo": "\"FACT\"",
        "fechaHoraEmision": "\"2023-06-16T18:28:11\"",
        "codigoMoneda": "\"GTQ\""
      },
      "emisor": {
        "direccion": {
          "direccion": "\"Ciudad\"",
          "codigoPostal": "\"01001\"",
          "municipio": "\"\"",
          "departamento": "\"\"",
          "pais": "\"GT\""
        },
        "nitEmisor": "\"107902281\"",
        "nombreEmisor": "\"SOCIEDAD ANONIMA\"",
        "codigoEstablecimiento": "\"0\"",
        "nombreComercial": "\"SOCIEDAD ANONIMA\"",
        "correoEmisor": "\"[email protected]\"",
        "afiliacionIVA": "\"GEN\""
      },
      "receptor": {
        "direccion": {
          "direccion": "\"Ciudad  \"",
          "codigoPostal": "1",
          "municipio": "\"\"",
          "departamento": "\"\"",
          "pais": "\"GT\""
        },
        "idReceptor": "\"CF\"",
        "nombreReceptor": "\"Consumidor Final\"",
        "correoReceptor": "\"[email protected]\""
      },
      "items": []
    }
  },
  {
    "items": {
      "bienOServicio": "\"B\"",
      "cantidad": 1,
      "unidadMedida": "\"UN\"",
      "descripcion": "T-Shirt Three",
      "precioUnitario": "15.00",
      "precio": "15.00",
      "descuento": "0.00",
      "total": "15.00",
      "impuestos": {
        "nombreCorto": "\"IVA\"",
        "codigoUnidadGravable": "1",
        "montoGravable": 13.392857142857142,
        "montoImpuesto": 1.607142857142857
      }
    }
  },
  {
    "items": {
      "bienOServicio": "\"B\"",
      "cantidad": 1,
      "unidadMedida": "\"UN\"",
      "descripcion": "Mug BHeart",
      "precioUnitario": "5.00",
      "precio": "5.00",
      "descuento": "0.00",
      "total": "5.00",
      "impuestos": {
        "nombreCorto": "\"IVA\"",
        "codigoUnidadGravable": "1",
        "montoGravable": 4.4642857142857135,
        "montoImpuesto": 0.5357142857142856
      }
    }
  }
]

and this is the expected output data:

  {
    "Dte": {
      "datosGenerales": {
        "tipo": "\"FACT\"",
        "fechaHoraEmision": "\"2023-06-16T18:28:11\"",
        "codigoMoneda": "\"GTQ\""
      },
      "emisor": {
        "direccion": {
          "direccion": "\"Ciudad\"",
          "codigoPostal": "\"01001\"",
          "municipio": "\"\"",
          "departamento": "\"\"",
          "pais": "\"GT\""
        },
        "nitEmisor": "\"107902281\"",
        "nombreEmisor": "\"SOCIEDAD ANONIMA\"",
        "codigoEstablecimiento": "\"0\"",
        "nombreComercial": "\"SOCIEDAD ANONIMA\"",
        "correoEmisor": "\"[email protected]\"",
        "afiliacionIVA": "\"GEN\""
      },
      "receptor": {
        "direccion": {
          "direccion": "\"Ciudad  \"",
          "codigoPostal": "1",
          "municipio": "\"\"",
          "departamento": "\"\"",
          "pais": "\"GT\""
        },
        "idReceptor": "\"CF\"",
        "nombreReceptor": "\"Consumidor Final\"",
        "correoReceptor": "\"[email protected]\""
      },
      "items": []
    }
  },
  {
    "items": {
      "bienOServicio": "\"B\"",
      "cantidad": 1,
      "unidadMedida": "\"UN\"",
      "descripcion": "T-Shirt Three",
      "precioUnitario": "15.00",
      "precio": "15.00",
      "descuento": "0.00",
      "total": "15.00",
      "impuestos": {
        "nombreCorto": "\"IVA\"",
        "codigoUnidadGravable": "1",
        "montoGravable": 13.392857142857142,
        "montoImpuesto": 1.607142857142857
      }
    }
  },
  {
    "items": {
      "bienOServicio": "\"B\"",
      "cantidad": 1,
      "unidadMedida": "\"UN\"",
      "descripcion": "Mug BHeart",
      "precioUnitario": "5.00",
      "precio": "5.00",
      "descuento": "0.00",
      "total": "5.00",
      "impuestos": {
        "nombreCorto": "\"IVA\"",
        "codigoUnidadGravable": "1",
        "montoGravable": 4.4642857142857135,
        "montoImpuesto": 0.5357142857142856
      }
    }
  }

I’m afraid I was sending an incorrect formatted JSON, after fixing the JSON I was able to send it without stringify or parse functions.

In the body field just typed:

{{ $input.first().json }}

and got the XML data as expected

2 Likes

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