Application/x-www-form-urlencoded -> str.replace is not a function

Dear all, hope you are well.

When using the node HTTP Request, with a basic post (see below), the API requests from me to have a header: “Content-Type”: “application/x-www-form-urlencoded”.
But as soon as I use it, I have this error:

str.replace is not a function
    at Querystring.rfc3986 (/usr/local/lib/node_modules/n8n/node_modules/request/lib/querystring.js:43:14)
    at Request.json (/usr/local/lib/node_modules/n8n/node_modules/request/request.js:1287:30)
    at Request.init (/usr/local/lib/node_modules/n8n/node_modules/request/request.js:406:10)
    at Request.RP$initInterceptor [as init] (/usr/local/lib/node_modules/n8n/node_modules/request-promise-core/configure/request2.js:45:29)
    at new Request (/usr/local/lib/node_modules/n8n/node_modules/request/request.js:127:8)
    at Object.request (/usr/local/lib/node_modules/n8n/node_modules/request/index.js:53:10)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/HttpRequest.node.js:797:51)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:416:62

I found a similar issue on github but the solution seems not to work for me (or I implemented it wrongly, sorry).

See my node below.

Thanks in advance,

{
  "nodes": [
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://api.helloasso.com/oauth2/token",
        "jsonParameters": true,
        "options": {},
        "bodyParametersJson": "={\n\t\"client_id\": \"{{$node[\"Set\"].parameter[\"values\"][\"string\"][2][\"value\"]}}\",\n\t\"grant_type\": \"refresh_token\",\n\t\"refresh_token\": \"{{$node[\"Set\"].parameter[\"values\"][\"string\"][0][\"value\"]}}\"\t\t\n}",
        "headerParametersJson": "{ \"Content-Type\": \"application/x-www-form-urlencoded\"}"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        530,
        120
      ]
    }
  ],
  "connections": {}
}

Did you try setting Body Content Type to Form Urlencoded?

That seems to be a problem caused by the underlying library. Does this workaround work?

{
  "nodes": [
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://api.helloasso.com/oauth2/token",
        "jsonParameters": true,
        "options": {
          "bodyContentType": "raw"
        },
        "bodyParametersJson": "={{ { \"client_id\": $node[\"Set\"].parameter[\"values\"][\"string\"][2][\"value\"], \"grant_type\": \"refresh_token\", \"refresh_token\": $node[\"Set\"].parameter[\"values\"][\"string\"][0][\"value\"] } }}",
        "headerParametersJson": "={{ { \"Content-Type\": \"application/x-www-form-urlencoded\"} }}"
      },
      "name": "HTTP Request3",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        950,
        690
      ]
    }
  ],
  "connections": {}
}

Thanks to both of you. I tried both but it hasn’t worked.

@jan: I have this error now :frowning:
ERROR: RequestError: Error: Argument error, options.body.

Error: RequestError: Error: Argument error, options.body.
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/HttpRequest.node.js:806:27)
    at async /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:416:47

@ivov: the body is not “understood” by the API as a consequence if I do that. It just tells me “client_id” is not set while it is actually included in the body …

hm can you try this:

{
  "nodes": [
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://api.helloasso.com/oauth2/token",
        "responseFormat": "string",
        "jsonParameters": true,
        "options": {
          "bodyContentType": "raw"
        },
        "bodyParametersJson": "={ \"client_id\": \"{{ $node[\"Set\"].parameter[\"values\"][\"string\"][2][\"value\"] }}\", \"grant_type\": \"refresh_token\", \"refresh_token\": \"{{ $node[\"Set\"].parameter[\"values\"][\"string\"][0][\"value\"] }}\" }",
        "headerParametersJson": "={{ { \"Content-Type\": \"application/x-www-form-urlencoded\"} }}"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        868,
        529
      ]
    }
  ],
  "connections": {}
}

Thanks again !

It still looks like the body is not recognized by the API. Here’s the error message I have:
ERROR: StatusCodeError: 400 - "{\"error\":\"unauthorized_client\",\"error_description\":\"client_id is not set\"}"

And is the client_id set? Does it resolve to a value in the UI? Because it receives it from a referenced Set-Node.

Yes, as you can see in the attached picture (fake value for client_id and refresh token for the picture)

If it gets displayed then it should be send.

The only thing I can think of is that you send the client_id wrong. So that the API for example expects it in the query parameter or header but you send it via body.

I’m sorry but I don’t think so.
it works fine on postman …

Did you try to also send the data the same way in n8n by adding the Option “Body Content Type” and setting it accordingly?

Hey Cyril!

Are you also setting the User-Agent header? For some APIs it is required that we set the User-Agent header.

Unfortunately, still have the same error issue …

Not sure to understand … when I use postman, I don’t think I have to add this header …

With the latest n8n-version ([email protected]) will it now display the request the HTTP Request node does in the browser console. Maybe that helps you to figure out what the difference is.

As alternative can you send both requests (from n8n and Postman) to a page like https://webhook.site to see the difference.

1 Like