HTTP POST special characters

Hello everyone.

I'm trying to make a POST request with an "http request" node, where the body is a JSON containing text with accents.

If I make a POST request with the body (without accents):

{
     "field_0": "GARCIA PEREZ, IVAN",
     "__metadata": {
       "type": "SP.Data.EmployeesListItem"
     }
}

The request is accepted, returns 201.

If you make a POST request with the body (with tildes):

{
     "field_0": "GARCÍA PÉREZ, IVÁN",
     "__metadata": {
       "type": "SP.Data.EmployeesListItem"
     }
}

The request is unsuccessful, returning 400: Invalid JSON. An unexpected end of input was found in the JSON content. Not all object and array scopes were closed.

How can I resolve this error so that I can send string values with accents in the body of the POST request?


Admin edit: surrounded code snippets with backticks

@carlos-conde Welcome to the forum!

  1. what version of NR and node.js are you running? (you can find this in the startup log)
  2. what platform are you running NR on?

I did a copy of the data you say is causing the error and sent it thru a json node and it had no problem with it.

Please create a small flow demonstrating your issue and then export it and attach it to a reply.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

Try adding the header
Content-Type application/json; Charset=UTF-8

Question: Are you sending a JS object in the payload OR making the JSON string yourself?

Hi, thank you for response

I have tried adding those headers, but it did not solve the problem.
I have tried sending a JSON object and also a JSON Object parsed to String, it has not solved the problem either.

But I have managed to make the POST with special characters using the request library in a function node.

Hi, thank you for response

I'm going to edit my post.

But I solved using request library

That is not good. Could you share both a non working flow (using HTTP node) and your working flow please? I would like to investigate this further.

I can't edit my post :frowning:

Anyway.

version node-red: 1.3.5
plataform: Ubuntu Server 22.04.4 LTS

example:

   [
    {
        "id": "b5f8496f.14d4c8",
        "type": "inject",
        "z": "e41869bb.09b5a8",
        "name": "ADD",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "20",
        "topic": "add",
        "payload": "",
        "payloadType": "date",
        "x": 250,
        "y": 200,
        "wires": [
            [
                "2ac302f7.1b52ee"
            ]
        ]
    },
    {
        "id": "2ac302f7.1b52ee",
        "type": "function",
        "z": "e41869bb.09b5a8",
        "name": "config req",
        "func": "try{\n    const body = {\n        \"Title\": \"01001989\",\n        \"field_0\": \"C@RLOS KÖNDE KANTÉ\"\n    }\n    const len = JSON.stringify(body).length;\n    \n    msg.payload = body;\n    // msg.payload = JSON.stringify(body);\n    \n    msg.headers = {\n        'Authorization': 'Barear + accestoken',\n        'Content-Type': \"application/json;\",\n        'Content-Length': len,\n        'X-RequestDigest': \"{form_digest_value}\"\n    }\n    \n    msg.url = \"https:/....sharepoint.com/.../web/lists/getByTitle('listName')/items\";\n    \n    node.status({ fill: \"green\", text: \"done\" });\n    return msg;\n}catch(err){\n    node.status({ fill: \"red\", text: \"error\" });\n    node.error(err);\n    throw err;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 200,
        "wires": [
            [
                "7af78bcc.1e27f4"
            ]
        ]
    },
    {
        "id": "7af78bcc.1e27f4",
        "type": "http request",
        "z": "e41869bb.09b5a8",
        "name": "add",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 550,
        "y": 200,
        "wires": [
            []
        ]
    }
] 

Solved:

   [
    {
        "id": "8be1e4a3.4be038",
        "type": "function",
        "z": "8e958c14.3560d",
        "name": "add",
        "func": "try{\n    const request = global.get('request');\n    \n    const url = 'https://.....sharepoint.com/..../_api/web/lists/getByTitle(\\'ListName\\')/items';\n    const data = {\n        \"Title\": \"01001989\",\n        \"field_0\": \"C@RLOS KÖNDE KANTÉ\"\n    }\n    const options = {\n        'method': 'POST',\n        'url': url,\n        'headers': {\n            'Authorization': 'Bearer + access_token',\n            'Accept': 'application/json;odata=verbose',\n            'X-RequestDigest': '{form_digest_value}',\n            'Content-Type': 'application/json'\n        },\n        body: JSON.stringify(data)\n    };\n    \n    request(options, function (error, response) {\n        if (response.statusCode === 201) {\n            node.status({ fill: \"green\", text: response.statusCode });\n            return response;\n        }\n        if (response.statusCode !== 201) {\n            node.status({ fill: \"yellow\", text: response.statusCode });\n            return response;\n        }\n        if (error) {\n            node.status({ fill: \"red\", text: \"error\" });\n            node.error(error);\n            return error;\n        }\n    });\n}catch(err){\n    node.status({ fill: \"red\", text: \"error\" });\n    node.error(err);\n    throw err;\n}",
        "outputs": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 530,
        "y": 300,
        "wires": [],
        "icon": "node-red/white-globe.svg"
    }
]

That is an old release of NR. The current release is 3.1.6

Are you certain that is correct? That version is 3 years old (ancient in terms of Node-RED)

Also what version of NodeJS are you using?

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