I鈥檓 using the sendgrid api to add recipients, the server response is good, though nothing is added. and thats because i鈥檓 using Django manager shell.
I used this script to add the recipient:
>> import sendgrid
>> service = sendgrid.SendGridAPIClient(apikey=API_KEY)
>> response = service.client.contactdb.recipients.post(
... request_body=[{
... 'created': 1234543,
... 'email': '[email protected]',
... 'first_name': 'John',
... 'last_name': '',
... 'gender': 'M',
... }]
...)
>> print(response.status_code)
201
>> print(response.body)
'{"new_count":1,"updated_count":0,"error_count":0,"error_indices":[],"unmodified_indices":[],"persisted_recipients":["dG90b0B0dXQuY29t"],"errors":[]}\n'
>> print(response.headers)
{'Server': 'nginx', 'Date': 'Wed, 01 Aug 2018 08:34:11 GMT', 'Content-Type': 'application/json', 'Content-Length': '149', 'Connection': 'keep-alive', 'Access-Control-Allow-Methods': 'HEAD, GET, PUT, POST, DELETE, OPTIONS, PATCH', 'Access-Control-Max-Age': '21600', 'Access-Control-Expose-Headers': 'Link, Location', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl, X-Recaptcha', 'Content-Security-Policy': "default-src https://api.sendgrid.com; frame-src 'none'; object-src 'none'", 'X-Content-Type-Options': 'nosniff', 'Strict-Transport-Security': 'max-age=31536000', 'X-Ratelimit-Remaining': '2', 'X-Ratelimit-Limit': '3', 'X-Ratelimit-Reset': '1533112452', 'Powered-By': 'Mako'}
I checked if it is added .. and nothing was added
I tried the same thing with postman, and everything worked normally with no problem, and i found the recipient.
After I tried with requests
>> import requests
>> url = 'https://api.sendgrid.com/v3/contactdb/recipients'
>> headers = {
... 'Content-Type': 'application/json',
... 'Authorization': 'Bearer %s' % API_KEY,
... 'Cache-Control': 'no-cache',
... }
>> data = json.dumps([{
... 'created': 1234543,,
... 'email': '[email protected]',
... 'first_name': 'John',
... 'last_name': '',
... 'gender': 'M',
... }])
>> response = requests.post(url, headers=headers, data=data)
>> response
<Response [201]>
>> response.text
'{"new_count":1,"updated_count":0,"error_count":0,"error_indices":[],"unmodified_indices":[],"persisted_recipients":["ZXhhbXBsZUBleGFtcGxlLmNvbQ=="],"errors":[]}\n'
Again .. nothing was added.
and all this time I was running these scripts in Django manager shell in dev mode,
after when I used the same scripts in python shell, it works correctly, and I recipient is added, so I have no idea whats going on !!!!!
I also tried with Django manager shell in prod mode, same problem !
I also reported this bug to Django
Hello @gothraven,
Thanks for submitting a GitHub issue! We are very sorry that you are running into this problem. In order to better serve you, as this does not present itself as a library specific issue, we would like to ask that you reach out to our support team at https://support.sendgrid.com.
Thank you!
SendGrid DX Team
The API does NOT fail when the request contains wrong data, i used to send a wrong datetime in my request, but the API did not say anything about that, it only accepted and said that everything is fine. and that is not the only thing we can send wrong and the API does not report anything, I somehow found out that it created new recipients but kept them hidden somehow, and i think it can be a security issue for you guys. 馃挘馃挜
Thanks for letting me know @gothraven!
Did you inform our support team as well?
No, I honestly forgot to do that. and it takes really long to describe the issue again 馃槬
OK, no problem @gothraven, thanks for following up!
@kylearoberts, please see the comment above (the one with the bomb in it :))
Hi @gothraven
I have read through this issue and will likely need more details as things are not adding up for me.
In the two tests that you showed in the start the email addresses you showed that you were adding did not match with the response you got from the system:
dG90b0B0dXQuY29t = [email protected]
ZXhhbXBsZUBleGFtcGxlLmNvbQ== = [email protected]
Since these do not match with the email you were trying to add something must not be working with the Django scripts you are using. It is likely best to take a closer look at those. In this case it sounds like Django is not passing on the body data like it should be.
The API does NOT fail when the request contains wrong data
I am going to need more details on this and some clear examples so I can replicate the issue.
i used to send a wrong datetime in my request
An example of this in cURL would greatly help.
I somehow found out that it created new recipients but kept them hidden somehow
Going to need more details on this as right now I have no idea how to replicate this issue. Once again a cURL request example would greatly help.
@thinkingserious
This might be an issue that is specific to Django since it is not replicated outside of that environment.
@kylearoberts well, I'm sorry, i must have copied the wrong log (100%), but I'm sure it did happened, and it had nothing to do with Django, though i had the same thoughts as you and i checked with the Django people, and they were so doll and so confident that it wasn't their issue.
Problem :
import sendgrid
from . import X
service = sendgrid.SendGridAPIClient(apikey='XXX')
x = X.objects.get(id=1)
service.client.contactdb.recipients.post(
request_body=[{
'created': int(x.created.timestamp()), # here is the problem
'email': x.email,
'first_name': x.first_name,
'last_name': x.last_name or '',
'gender': x.gender,
}]
)
Solution :
import sendgrid
from . import X
service = sendgrid.SendGridAPIClient(apikey='XXX')
x = X.objects.get(id=1)
service.client.contactdb.recipients.post(
request_body=[{
'sign_up_date': int(x.created.timestamp()), # now everything is fine
'email': x.email,
'first_name': x.first_name,
'last_name': x.last_name or '',
'gender': x.gender,
}]
)
Hi @gothraven
Thanks for the additional details. So from these last details, the issue looks to be with custom fields. This is something we likely need to get a closer look into to make sure everything is functioning. Could you please create a support ticket here? We are going to need to look at your specific account and how you have your custom fields setup.
This seems to be happening for me as well, trying to add recipients within a Flask app. My understanding of the above is that despite no contact being created, the API is still returning a 201?? How can I tell if the request was successful or not..?
Hello @dhhagan,
Are you still experiencing this issue? If so, could you please open a new issue and provide us with your source code so that we may try to reproduce?
Thanks!
With Best Regards,
Elmer
Most helpful comment
The API does NOT fail when the request contains wrong data, i used to send a wrong
datetimein my request, but the API did not say anything about that, it only accepted and said that everything is fine. and that is not the only thing we can send wrong and the API does not report anything, I somehow found out that it created new recipients but kept them hidden somehow, and i think it can be a security issue for you guys. 馃挘馃挜