Webhook to python script with raw body

Describe the problem/error/question

New n8n user, and I am testing flow where a webhook that will received a raw body. I need to send this raw body to my python script which is being called via execute command node. I tried to send the body from the webhook as a arg when calling the script but that does not work.

Please share your workflow

Python Script:

import sys

items = sys.argv
print(items)

Execute Command:

python py_scripts/test.py {{ $json.body }}

The json.body from web hook:


"body":
{
  "this": "test",
  "body": "should",
  "be": "the",
  "response": "body"
}

Execute Command Node Output:

[
  {
    "exitCode": 0,
    "stderr": "",
    "stdout": "['py_scripts/test.py', '[object', 'Object]']"
  }
]

As you can see it sending the object as a string. I am assuming this be because i am sending as command argument. However I do not know how else to have my python script ingest the webhook body object.

Information on your n8n setup

  • n8n version: 0.227.0
  • Database (default: SQLite): SQLite
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Debian

Hi @Varun_Panchal, welcome to the community :tada:

Your webhook body is a JSON object, and [object Object] is its string representation in JavaScript. So based on the information provided so far the behavior might be exactly what is it expected. To get this right would require knowing which data type and format exactly is required by your script and how you’ve set up your workflow.

Assuming you want a string rather than an object, and don’t want any n8n/JavaScript logic to be applied to your webhook body, you could enable the “Raw Body” option on the webhook node:

This would provide you with a binary object data which contains the original body. You can then use a subsequent Move Binary Data node to read this body as a string. In a workflow this would like so:

You’d then get a string with your body in the data field which could be what you’re looking for. Simply use {{ $json.data }} to access this field in your Execute Command node (though again, this would depend exactly on what your script needs, based on the example result you have shared it looks like it simply returns all parameters):

Hope this helps :slight_smile:

1 Like

@MutedJam Thank you, this is what I needed! I wanted to pass the json object into my python script as a string!

2 Likes

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