am getting bad request after adding template_id
this is the code am using
sg_api = sendgrid.SendGridAPIClient(apikey=settings.SENDGRID_API_KEY)
substitutions = {}
to_emails = [{'email': to_email} for to_email in to_emails]
if subs:
for substitution in subs:
substitutions['%' + substitution + '%'] = subs[substitution]
data = {
'personalizations': [
{
'to': to_emails,
'subject': subject,
'substitutions': substitutions
},
],
'from': from_email,
'template_id':'[template_id]'
}
if template is not None:
template = data['template_id']
response = sg_api.client.mail.send.post(request_body=data
return response
Hello @naiyoma,
When using templates, please follow this example.
Please let me know if that helps.
With Best Regards,
Elmer
@thinkingserious
i tried the example code but:
response = sg.send(message)
returns 'SendGridAPIClient' object has no attribute 'send'
I also tried this
response = sg_api.client.mail.send.post(request_body=message)
Hello @naiyoma,
It looks like you may need to update your SendGrid client.
Please try: pip install sendgrid-python --upgrade to get v6.0.3. Then you should be able to use this example.
Thanks!
I upgraded sendgrid but i noticed that my class SendGridAPIClient lacks def send
Hello @naiyoma,
What version do you see in sendgrid/VERSION.txt?
sendgrid==5.4.1
sendgrid-python=0.1.1
managed to upgrade using pip install --upgrade sendgrid
Hello @naiyoma,
The version needed should be v6.0.3.
What happened when you ran the upgrade command?
With Best Regards,
Elmer
hey @thinkingserious the first command didn't upgrade but the second one worked
Does the version sendgrid/VERSION.txt equal 6.0.3?
sendgrid==6.0.4
@thinkingserious is it not possible to send the templete_id using sendgrid version 5
Can you please describe your development environment? (e.g. are you using virtuanenv, docker, hosting provider, etc...).
hey @thinkingserious i was able to get the specific error
which was {'errors': [{'message': 'Invalid type. Expected: string, given: array.', 'field': 'personalizations.0.subject', 'help': 'http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.subject'}]}
I placed a comma after the subject therefore it was returning an array and not a string
issue resolved
Awesome! Thank you for the follow up :)
also one shouldn't use substitutions with transactional templates
Im having the same issue... @naiyoma how did you get this detailed error response?
from urllib.error import HTTPError
from django.http import HttpResponse
import python_http_client
try:
response = sg_api.client.mail.send.post(request_body=data)
except (HTTPError, python_http_client.exceptions.BadRequestsError) as error:
return HttpResponse(error)
so with that you can use a print statement print(error.body) or pdb and still check for the error.body
Most helpful comment
so with that you can use a print statement
print(error.body)or pdb and still check for theerror.body