Body payload as binary data from webhook

Hello,

I have a webhook that provides me with a body in JSON form. For authentication the manufacturer provides a signature code. This is calculated with crypto. The calculation needs the raw data of the body.

My problem with the calculation is that the internal conversion to JSON will result in a change of the content. See the example:

“fNumber”:8.0, → becomes “fNumber”:8,

I think I have already tried everything, but I can’t get the raw data into the value field of crypto. These are my tests:

  • {{JSON.stringify($json[“body”])}} → convert issue 8.0 becomes 8
  • {{$binary.data}} → it is not the data content it is: [Object: {“mimeType”:“application/json”}]
  • Move Binary Data - Modul → same converting error.
  • Set Webhook with Option “Binary Data”, the hook is not triggered. Only when i set the option to “Raw Data”. (“content-type”: “application/json”)

How can i get the original raw data from the Body of the Request to put it into the Value from Crypto?

Information on your n8n setup

  • **n8n version:latest on-premise
  • **Database you’re using (default: SQLite):SQLite
  • **Running n8n with the execution process [own(default), main]:own
  • **Running n8n via [Docker, npm, n8n.cloud, desktop app]:npm

Hi @Hans_Wurst, first of all, welcome to the community :tada:

I’m sorry to hear you’re having trouble.

This would be because n8n is a Javascript application and there is no difference between 8.0 and 8 in JavaScript, both would be considered of type number. You will find the same behaviour outside of n8n as well (e.g. when typing console.log(8.0) in your browser console it prints 8). This is also reflected in JSON where no distinction between integer and floating-point types exists.

So to preserve the original value, you would need to work with - for example - a String instead of JSON. Would this approach work for you?

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "8c4e4e30-ed48-4a48-b4bd-fd65734a5bde",
        "options": {
          "rawBody": true
        }
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        150
      ],
      "webhookId": "8c4e4e30-ed48-4a48-b4bd-fd65734a5bde"
    },
    {
      "parameters": {
        "action": "hmac",
        "type": "SHA256",
        "value": "={{$json[\"source_data\"]}}",
        "dataPropertyName": "target_data",
        "secret": "foo"
      },
      "name": "Crypto",
      "type": "n8n-nodes-base.crypto",
      "typeVersion": 1,
      "position": [
        850,
        150
      ]
    },
    {
      "parameters": {
        "setAllData": false,
        "destinationKey": "raw_data",
        "options": {}
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        450,
        150
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "source_data",
              "value": "={{$json[\"raw_data\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        650,
        150
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Crypto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

When POSTing a JSON body to my webhook URL like this:
image

It was preserved all the way to the crypto node:

Hi @MutedJam ,

this approach works great! The Destination Key did the trick!

Now i can access the raw data as string and crypto encrypt the right data.
Thank you very much.

PS: And i didn’t need the Set node or does this have another special feature that I do not know?

2 Likes

Awesome, glad to hear this works, thanks a lot for confirming!

I was in a rush when I shared the example and was first experimenting using the Set node - if it works without the Set node, just throw it away :smiley: