Why isn't this code extracting URL parameters correctly?

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

I tried this but nothing seems to work.
import urllib.parse

Extract variables from Pipedream trigger

url_param = urllib.parse.unquote(event[‘body_raw_url’])
query_params = urllib.parse.parse_qs(urllib.parse.urlparse(url_param).query)
license_plate = query_params.get(‘licenseplate’, [])[0]
mileage = query_params.get(‘mileage’, [])[0]
email = query_params.get(‘email’, [])[0]

Use variables in your code

print(f"License Plate: {license_plate}“)
print(f"Mileage: {mileage}”)
print(f"Email: {email}")

Hi thanks for sharing that code snippet. Did you happen to delete the def handler function wrapper too?

You’ll need to make sure to keep that wrapping function, so that way the system can pass in your steps context

Hi Pierce, thank you for your kind and quick response. I will try : )

It keeps telling me ‘Pipedream’ object has no attribute ‘trigger’
I was using this function

def handler(pd: “pipedream”):
# Extract the query string from the URL
query_string = pd.trigger.metadata.query_string

# Extract everything after the ?
query_param = query_string.split("?")[-1]

# Return the extracted value
return {"query_param": query_param}

For now, I am just trying to extract query parameters from the trigger but somehow I can’t reference it at all

please show an example of how to use a Python code step to access the query string params from an incoming HTTP request?

Mario was totally wrong on that one.

But basically you’ll need to use the pd.steps dictionary not the pd.trigger attribute.

So the HTTP event data will be available at:

pd.steps['trigger']['event']

One gotcha I’ve run into that I’ll mention is that I’ll rename a workflow step and then any code referencing it needs to change. In the case of triggers, they can’t be renamed. But if you use pd.steps['myworkflowstep']['event'] and then later change myworkflowstep to andreaworkflow you will have to touch any code to make it pd.steps['andreaworkflow']['event']

Hi Pierce, for some reason I can’t do it. Would you please be so kind to share how you would use that to print the query parameter. Let’s say this is the trigger https://eoy10v524fteh67.m.pipedream.net and I would like to extract the query parameter portion https://eoy10v524fteh67.m.pipedream.net?ciao so that the python function returns just ciao and prints it in python?

Hi thank you for the tip :slightly_smiling_face:

I don’t mean to step on a conversation Pierce is in though since I’m hear I’d like to pay it forward for help I’ve received. Take a look at the following screenshot. This is an example of what I see with a webhook example when I add a parameter to it

Better yet, zoom in on this part

You could use something like pd.steps[trigger][event][query]

and you would get a json or list of all items under query.

Or if your parameters are consistent, you could do pd.steps[trigger][event][query][myparameter]

If you just want to use the URL and parse it all yourself, you could use pd.steps[trigger][event][url]