Webhook: req.rawBody is undefined

Hello,

I am trying to validate the signature of the requests sent by Slack on my webhook. For this, I need to retrieve the rawBody of the request to be able to generate the signature and compare it with the original one. However, whenever I am setting the parameter “rawBody” to true in my webhook, the following error is triggered:

Digging deeper, the error stems from the line data: req.rawBody.toString(n8n_core_1.BINARY_ENCODING), (file: Webhook.node.js)

Best,
Laurent

Welcome to the community @laurent!

Not sure if I would call it a bug or expected behavior. For me it works totally fine as long as the request actually has a body. So doing this works:

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:5678/webhook-test/ed8891c3-114a-43ad-827f-e755556b0887

I get the same error as you if I send a request without a body like this:

curl -H "Content-Type: application/json" -X POST http://localhost:5678/webhook-test/ed8891c3-114a-43ad-827f-e755556b0887

So if the request sends data, it works fine. If it does not have any data, then it errors.

I added now code which makes sure that “rawBody” does always get set. So it does at least not error anymore:
https://github.com/n8n-io/n8n/commit/7259aa4cf69080fff24cbac23e63823c4ef56012
That will be released with the next version.

In your case it seems however strange that Slack makes a POST request without any data at all.

Hi Jan,

Thank you for this prompt reply. Without passing the option -H "Content-Type: application/json", I can reproduce the error. I am not sure why the “rawBody” option excepts the request payload to be in JSON.

Example: curl -X POST -d '{"key1":"test"}' http://localhost:5678/webhook-test/ed8891c3-114a-43ad-827f-e755556b0887

Best regards,
Laurent

Do not understand. It will currently error no matter if you send a “Content-Type” header or not.

Hi Jan,

Sorry for the confusion. I am trying to get the original payload of a POST request sent to my Webhook, but I am struggling. The Webhook is supposed to retrieve all the actions that have been occurred in a Slack channel and for each action, Slack sends a request to the Webhook. However, for validating the requests, Slack is asking us to compute the HMAC SHA256 of the request’s raw body and this is where I am struggling, because if I am enabling the rawBody, I get “req.rawBody is undefined” and if I am disabling it, I am getting a JSON output :frowning:

POST request sent by Slack to the Webhook
‘host’: ‘…’,
‘x-forwarded-proto’: ‘http’,
‘x-nginx-proxy’: ‘true’,
‘connection’: ‘close’,
‘content-length’: ‘2014’,
‘user-agent’: ‘Slackbot 1.0 (+https://api.slack.com/robots)’,
‘accept-encoding’: ‘gzip,deflate’,
‘accept’: ‘application/json,/’,
‘x-slack-signature’: ‘v0=5996b635b00471da856052823daf540…’,
‘x-slack-request-timestamp’: ‘1598623354’,
‘content-type’: ‘application/x-www-form-urlencoded’

payload=%7B%22type%22%3A%22interactive_message%22%2C%22actions%22%3A%5B%7B%22name%22%3A%22…

Input from the webhook is as follows (without rawBody option enabled).

How can I get the original form of the request (i.e. “payload=%7B%22type%22%3A%22interactive_message”) ?

Ah OK. Found the problem. It did not save the rawBody for “application/x-www-form-urlencoded” requests. Did just release a new bug-fix version [email protected] which works now as expected.

Be however aware that the data is really raw. Meaning it is not in the JSON-data of the item it is in the binary-data. So if you want want to “move” it over and get it displayed as “text” you would have to do the following:

Thank you very much jan :wink:

1 Like