sendgrid / python-http-client

Twilio SendGrid's Python HTTP Client for calling APIs
https://sendgrid.com
MIT License
126 stars 101 forks source link

HTTP 400 on second mail send #133

Closed Seluj78 closed 4 years ago

Seluj78 commented 4 years ago

Hi, I'm getting a 400 from this library when using sendgrid to send emails, but only after i've successfully sent a first email.

24-11-2019:13:59:20,571 INFO     [_internal.py:122] 127.0.0.1 - - [24/Nov/2019 13:59:20] "POST /v1/auth/user/forgot_password HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/python_http_client/client.py", line 174, in _make_request
    return opener.open(request, timeout=timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 531, in open
    response = meth(req, response)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 641, in http_response
    'http', request, response, code, msg, hdrs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 569, in error
    return self._call_chain(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 649, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/seluj78/Projects/VisionaeAPI/visionae_api/routes/v1/authentication.py", line 84, in user_password_reset
    send_password_reset_mail(data['email'])
  File "/Users/seluj78/Projects/VisionaeAPI/visionae_api/utils/mail.py", line 55, in send_password_reset_mail
    sendmail(recipients=user_email, subject="Password reset for Visionae", html=html)
  File "/Users/seluj78/Projects/VisionaeAPI/visionae_api/utils/mail.py", line 32, in sendmail
    from_email=from_email, to_email=recipients, subject=subject, html=html
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/flask_sendgrid.py", line 74, in send_email
    return self.client.mail.send.post(request_body=self.get())
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/python_http_client/client.py", line 262, in http_request
    self._make_request(opener, request, timeout=timeout)
  File "/Users/seluj78/Projects/VisionaeAPI/venv/lib/python3.7/site-packages/python_http_client/client.py", line 178, in _make_request
    raise exc
python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request

In this log you can see the first request going fine, then the second ending up with a 500 because of the 400 raised by this library. Any idea why ?

Seluj78 commented 4 years ago

As I stated on my sendgrid support, it seems like flask_sendgrid, on the second request, sends twice the to parametter

childish-sambino commented 4 years ago

Closing as the issue is with flask_sendgrid since it's doubling the to parameter: https://github.com/frankV/flask-sendgrid/issues/19

Including the same to address results in a 400 (source).