Best answer

Dynamic Variable in Python

  • 19 May 2021
  • 2 replies
  • 443 views

Userlevel 1

Hello!

I’m having trouble trying to include an input data variable to my JSON string using Python - Requests.

  • Trigger: Catch’s hook from a web form
  • Action: Consumes API to send a Confirmation email

Input Data

These are the fields the Zap retrieves from the webhook

 

Code (Action that sends de confirmation email):

import requests

url = "https://api.trx.icommarketing.com/email"

payload="{ \r\n \"From\": \"Icomm Test demo@demo.com\", \r\n \"To\": \"lucas.vargas@icommkt.com\", \r\n \"Cc\": \"\", \r\n \"Bcc\": \"\", \r\n \"Subject\": \"Hello :)\", \r\n \"Tag\": \"string\", \r\n \"HtmlBody\": \"Cuerpo del correo\", \r\n \"TextBody\": \"Texto plano\", \r\n \"ReplyTo\": \"daniel.alarcon@icommkt.com\", \r\n \"Headers\": [], \r\n \"TrackOpens\": true, \r\n \"TrackLinks\": \"HtmlAndText\", \r\n \"Attachments\": [ \r\n { \r\n \"Name\": \"readme.txt\", \r\n \"Content\": \"dGVzdCBjb250ZW50\", \r\n \"ContentType\": \"text/plain\" }, \r\n { \r\n \"Name\": \"report.pdf\", \r\n \"Content\": \"dGVzdCBjb250ZW50\", \r\n \"ContentType\": \"application/octet-stream\" \r\n } \r\n ]\r\n }"
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'X-Trx-Api-Key': 'PRIVATE KEY'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

When I test the API it works because it sends an email "To\": \"lucas.vargas@icommkt.com\"

However, I want the email to be sent to the person who filled the form using “email” as a dynamic variable.

 

Hope someone can help me with this case

 

Regards

 

icon

Best answer by GetUWired 20 May 2021, 14:30

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +12

Hi @LucasIcomm 

There may be more elegant ways to do this but the simplest way would just be to include the email variable in the payload you’ve defined

 

payload="{  \r\n    \"From\": \"Icomm Test demo@demo.com\",  \r\n    \"To\": \""+input_data['email']+"\",  \r\n    \"Cc\": \"\",  \r\n    \"Bcc\": \"\",  \r\n    \"Subject\": \"Hello :)\",  \r\n    \"Tag\": \"string\",  \r\n    \"HtmlBody\": \"Cuerpo del correo\",  \r\n    \"TextBody\": \"Texto plano\",  \r\n    \"ReplyTo\": \"daniel.alarcon@icommkt.com\",  \r\n    \"Headers\": [],  \r\n    \"TrackOpens\": true,  \r\n    \"TrackLinks\": \"HtmlAndText\",  \r\n    \"Attachments\": [    \r\n    {      \r\n        \"Name\": \"readme.txt\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"text/plain\"    },    \r\n    {      \r\n        \"Name\": \"report.pdf\",      \r\n        \"Content\": \"dGVzdCBjb250ZW50\",      \r\n        \"ContentType\": \"application/octet-stream\"    \r\n    }  \r\n    ]\r\n    }"

 

Userlevel 1

Thank you GetUwired. This code worked perfectly.

 

I appreciate your help

 

Have a very nice day