开发者

Need to make an HTTP call to send email and get response

开发者 https://www.devze.com 2023-02-09 08:20 出处:网络
I\'m writing a Sinatra app that will be sending an email using SendGrid.They provide a REST API for sending out an email and I wanted to find out the best way to mak开发者_StackOverflow中文版e a call

I'm writing a Sinatra app that will be sending an email using SendGrid. They provide a REST API for sending out an email and I wanted to find out the best way to mak开发者_StackOverflow中文版e a call to that API and get the response.

The format of the call is:

https://sendgrid.com/api/mail.send.xml?api_user=youremail@domain.com&api_key=secureSecret&to=destination@example.com&toname=Destination&subject=Example%20Subject&text=testingtextbody&from=info@domain.com

The response is in XML, which I need to parse to check for success/fail. More info at http://sendgrid.com/documentation/ApiWebMail

What's the best way to accomplish making the call and parsing the return?


Ruby's built in Net::HTTP library is plenty capable, but I like HTTPClient because it's got a nice set of simplified commands. The built-in open-uri is very convenient too if you don't need to do much heavy lifting.

For parsing HTML and XML, it's really hard to beat Nokogiri.

Ruby's got a URL parser called URI built-in, but I like Addressable better, especially if I'm going to be messing with the query parameters a lot.

Here's some samples of using the various gems:

require 'addressable/uri'
require 'nokogiri'
require 'open-uri'

If you want to manipulate the query parameters easily then Addressable::URI makes it convenient:

url = 'https://sendgrid.com/api/mail.send.xml?api_user=youremail@domain.com&api_key=secureSecret&to=destination@example.com&toname=Destination&subject=Example%20Subject&text=testingtextbody&from=info@domain.com'
uri = Addressable::URI.parse(url)
uri.query_values # => {"api_user"=>"youremail@domain.com", "api_key"=>"secureSecret", "to"=>"destination@example.com", "toname"=>"Destination", "subject"=>"Example Subject", "text"=>"testingtextbody", "from"=>"info@domain.com"}

You could assign that hash to a variable, modify values, then reassign them using uri.query_values=.

If you want a simpler interface than HTTPClient, Ruby's Open-URI is about as easy as they come. This would send a request to the url, returning the results to Nokogiri for parsing as XML:

doc = Nokogiri::XML(open(uri.to_s))

The returned XML response is 'sposed to look like this according to the site you linked to:

returned_xml = '<result>
  <message>success</message>
</result>'

So, if we parse that:

doc = Nokogiri::XML(returned_xml)

We can get at the response easily:

doc.at('message').inner_text # => "success"

I can't show a full round-trip example because I don't have an account there, but that should get you rolling.


The answer really depends on you. What language do you want to use? What frameworks (if any) appeal to you? You could do the entire thing using CURL from the command line if you want to do this as a one-off. Or you could wrap CURL in a Perl script. Or you could write Python or Ruby. Or you could write Java or .Net.

Or you could just hit that URL in your browser after filling in the appropriate query string parameters.

Every major language has many XML parsers, so that part should be fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号