sendgrid-ruby: Running "Hello Email" sample code gets a 400 error response from the API
Issue Summary
I am attempting to set up my Sendgrid integration and as a first step I copied the Hello Email code on this project’s root page, changed the email address to be valid, and then ran it in the Rails console. (The environment variable for my API key is valid, as well.) I get the following error:
2.3.1 :040 > response = sg.client.mail._('send').post(request_body: mail.to_json) => #<SendGrid::Response:0x007f9df0fdfa80 @status_code="400", @body="{\"errors\":[{\"message\":\"Invalid type. Expected: object, given: string.\",\"field\":\"(root)\",\"help\":\"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Request-Body-Parameters\"}]}", @headers="{\"server\"=>[\"nginx\"], \"date\"=>[\"Mon, 20 Jun 2016 19:29:20 GMT\"], \"content-type\"=>[\"application/json\"], \"content-length\"=>[\"191\"], \"connection\"=>[\"close\"], \"x-frame-options\"=>[\"DENY\"]}">
Steps to Reproduce
- Copy “Hello Email” code into text editor.
- Replace to: email address with valid personal email address.
- Paste the modified code into the Rails console.
Technical details:
- sendgrid-ruby Version: 3.0.4
- Ruby Version: 2.3.1
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 2
- Comments: 16 (4 by maintainers)
Alright, I figured it out. It’s an embarrassing “LRN 2 code n00b” moment. Mail.new is being hijacked in Rails by ActionMailer. Instead of trying to figure out how to hand that back to the Sendgrid gem I can just specify
SendGrid::Mail.newand that does the trick.Sorry for the dumb question. I’ll resolve the issue.
Not dumb at all, and it’s greatly appreciated that you took the time to share the solution. I’m sure you’ll help someone who is having a similar issue in the future.
@safetymonkey - thanks a lot for your help! You saved me a lot of troubleshooting 😃
Ah. I’m glad it helped!
Oh don’t worry @safetymonkey, I solved my problem by specifying
SendGrid::Mail.newlike you did. I was just thanking you for your documentation efforts back in June…Hello, n00b here. 😃 Had a similar issue in my production environment (but worked fine in dev), this thread helped me a lot. So just confirming “you’ll help someone who is having a similar issue in the future”. Thanks a lot @safetymonkey.
Was creating a rake task to test the mailer, which gave the same error. Fixed it with adding the SendGrid:: before all the lines with New. Thanks @safetymonkey!
@safetymonkey - thanks a lot, run into similar issue implementing sendgrid on my local dev and coming across his thread and your solution helped