400 Bad Request When Attaching PDF
I'm trying to email a PDF generated by Weasyprint but receiving an error HTTP Error 400: Bad Request which while not very descriptive I believe is due to an issue with the encoding of the attachment.
html_page, css_page = generatePDF(url) # Generates HTML and CSS from URL
pdf = html_page.write_pdf(stylesheets=css_page) # Compiles PDF from HTML and CSS as bytes string
pdf = base64.b64encode(pdf).decode() # Base64 encodes PDF
data = {
'personalizations' : [
{
'to' : [
{
'email' : data['to']
}
],
'subject' : data['subject']
}
],
'from' : {
'email' : data['from']
},
'content' : [
{
'type' : 'text/plain',
'value' : data['text']
},
{
'type' : 'text/html',
'value' : '<html><p>{}</p></html>'.format(data['html'])
}
],
'reply_to' : {
'name' : '{}'.format(sender_name),
'email' : '{}'.format(sender_email)
},
'attachments' : {
'content' : pdf,
'filename' : data['filename'],
'type' : 'application/pdf'
}
}
sg = sendgrid.SendGridAPIClient(apikey = SENDGRID_API_SECRET)
rq = sg.client.mail.send.post(request_body = data)
I've found a similar issue here but the posted solution did not resolve my problem. Thanks.
Technical details:
- sendgrid-python Version: 3.6.0
- Python Version: 3.5.1
Hello @apardes,
It looks like you are missing a few parameters for the attachments object, please see this example.
You may also want to try this for a better error message.
I hope that helps, Thanks!
@thinkingserious Thanks for the response!
Per the documentation here the only required parameters for the attachments object are content and filename. Please advise if this is not the case.
@apardes,
Thank you for responding so quickly. You are correct, those other parameters are not required.
- What is the error message you are receiving after trying my suggestion?
- To check if the encoding is correct, you may want to compare you output with the following (from your command line):
base64 -i <pdf> -o <encoded_pdf>
@thinkingserious All I receive is HTTP Error 400: Bad Request and the encoding is correct.
Can you please send the file that you are trying to send to [email protected] so we can investigate further? Thanks!
I having that exact same problem with the new api. With different files, i can attach, but the generated pdf by Weasyprint i get
{"errors":[{"message":"The content value must be a string at least one character in length.","field":"content.0.value","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content.value"}]}
and I am pretty sure that the content value has at least one char in length and is a string.
@thinkingserious I have sent an example file to the provided email. Thanks for your help.
@rittersport3 That's interesting. The errors I've been getting are not so explicit. What version of sendgrid-python are you using?
@apardes sendgrid==3.6.0 But what would that have to do with the Api response? I imagine that message comes from sendgrid, not from the library.
@thinkingserious Were you able to learn anything from the example file I sent?
Hello @apardes,
Regarding @rittersport3's comments (thanks!), you can extract those messages like so.
With regards to your mailed attachment, I have received it; however, this issue is still working its way up our backlog. I will be posting updates here as they are available.
Thanks!
I have same issue. Even I have tried encode using base64 -i <pdf> -o <encoded_pdf> then I copied directly into attachment.set_content(data) still HTTP Error 400: Bad Request :( is there any progress about the problem?
@altugank,
I have added your vote to this issue to help it raise higher on our backlog. Thanks!
We now have an attachment example here: https://github.com/sendgrid/sendgrid-python/blob/master/USE_CASES.md#attachment
The above link doesn't work. Here is the updated link https://github.com/sendgrid/sendgrid-python/blob/master/use_cases/attachment.md