Why am I unable to use data from the HTTP trigger step in my Python code within a Pipedream workflow?

This topic was automatically generated from Slack. You can find the original thread here.

Hi and @U05A7KLNALC I have created a workflow with 2 steps: a HTTP trigger and a Python code. I want to use data coming from the first step in the Python code. I’m trying to use the sample code I found in the doc:
pokemon_name = pd.steps["trigger"]["event"]["name"]
So I created this code in my Python step:
def handler(pd: "pipedream"):
# Reference data from previous steps
print(pd.steps["trigger"]["context"]["id"])
content = pd.steps["trigger"]["event"]["content"]
print(content)
# Return data for use in future steps
return {"foo": {"test": True}}

However I’m having this error message:

Traceback (most recent call last):

  File "/nano-py/pipedream/worker.py", line 137, in execute
    user_retval = handler(pd)
                  ^^^^^^^^^^^

  File "/tmp/__pdg__/dist/code/4dba53101b02ca099868a26b5028d98ba9567052216de5c4c5e6b49aa39ee797/code.py", line 4, in handler
    content = pd.steps["trigger"]["event"]["content"]
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^

KeyError: 'content'

I’m using this code from my local machine:
import http.client

text = "content line\n another content line"

body = f'{{"content": "{text}"}}'

conn = http.client.HTTPSConnection('[mycodexwsgsgwjsj.m.pipedream.net](http://mycodexwsgsgwjsj.m.pipedream.net)')
conn.request("POST", "/", body, {'Content-Type': 'application/json'})

What am I missing?

Based on the error message you’re receiving, it looks like the key 'content' does not exist in the event object of the data coming from the trigger step. This can happen if the POST request body sent to your HTTP trigger does not contain a content key, or if there’s a mismatch in the expected structure of the data. Given the code snippet you’re using to send data to your workflow: import http.client text = "content line\n another content line" body = f'{{"content": "{text}"}}' conn = http.client.HTTPSConnection('[mycodexwsgsgwjsj.m.pipedream.net](http://mycodexwsgsgwjsj.m.pipedream.net)') conn.request("POST", "/", body, {'Content-Type': 'application/json'}) This part looks correctly formatted, assuming the URL ([mycodexwsgsgwjsj.m.pipedream.net](http://mycodexwsgsgwjsj.m.pipedream.net)) is correct. Your Python code to send data to the HTTP trigger includes a content key in the JSON body, so the issue might be in how you’re accessing this data in your workflow’s Python code step. The correct way to access the content key in your Python code step should be as follows: def handler(pd: "pipedream"): # Reference data from previous steps print(pd.steps["trigger"]["context"]["id"]) # Ensure "event" has "content" before trying to access it content = pd.steps["trigger"]["event"].get("content", "Default content if not found") print(content) # Return data for use in future steps return {"foo": {"test": True}} Here, I’ve used the .get() method to safely access the content key from the event dictionary. This method will return None (or a default value you specify, in this case, "Default content if not found") if the content key does not exist, preventing the KeyError. Please ensure that the HTTP request is correctly formatted and sent to the correct endpoint URL of your Pipedream workflow. If the problem persists, double-check the payload structure being sent from your local machine to ensure it matches what your workflow expects.

Ok . I’m trying to process the data received from the HTTP trigger in Python, and return to my app. I have done this until now:
local machine code:

import http.client
import urllib.parse

params = {'test': 'nuevo_valor'}
query_string = urllib.parse.urlencode(params)

conn = http.client.HTTPSConnection('[eoxfdcj22c033nm.m.pipedream.net](http://eoxfdcj22c033nm.m.pipedream.net)')
conn.request("GET", f"/?{query_string}")
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
print(data.decode("utf-8"))

response from server:
200 OK

Success!

To customize this response, check out our docs here

python in workflow:

def handler(pd: "pipedream"):
    # Reference data from previous steps
    print(pd.steps["trigger"]["context"]["id"])
    content = pd.steps["trigger"]["event"]["body"]
    print(content)
    pd.respond({
      "status": 200,
      "body": {
        "message": "Everything is ok"
      }
    })
    # Return data for use in future steps
    return {"foo": {"test": True}}

I would like to be able to get the response from the server in my code. I have been reading the docs in the proposed link but didn’t find the answer

Based on your description, it seems you’re looking to process data received from an HTTP trigger in a Python step within a Pipedream workflow, and then return a custom response to your app. You’ve successfully received a response from the server, but you’re looking to customize this response further. To customize the HTTP response from your Pipedream workflow, you can use the pd.respond() method within your Python code step. This method allows you to send a custom HTTP response back to the client that initiated the request. It looks like you’re already using pd.respond() correctly in your workflow: def handler(pd: "pipedream"): # Reference data from previous steps print(pd.steps["trigger"]["context"]["id"]) content = pd.steps["trigger"]["event"]["body"] print(content) pd.respond({ "status": 200, "body": { "message": "Everything is ok" } }) # Return data for use in future steps return {"foo": {"test": True}} However, to ensure that your workflow returns this custom response, you need to make sure your HTTP trigger is configured to allow a custom response. This is a setting within the HTTP trigger itself. Here’s how you can check and enable this setting: 1. Go to your Pipedream workflow. 2. Click on the HTTP trigger step at the start of your workflow. 3. Look for a setting or option that mentions allowing custom responses. This might be phrased as “Respond with custom response” or similar. 4. Ensure this option is enabled. Once this is set up, your workflow should return the custom response defined in pd.respond() to your app. If you’re still facing issues, or if the response isn’t what you expected, please ensure that the pd.respond() method is called correctly and that the body and status keys are included in your response object, as mentioned in the Pipedream Python documentation. The body must be a JSON serializable object. For more detailed guidance or if you encounter specific issues not covered by the documentation, consider visiting Pipedream Support for more personalized assistance.