You must make an HTTP call to send an email and receive a response

I am writing a Sinatra application that will send an email using SendGrid. They provide a REST API for sending emails, and I would like to find a better way to make a call to this API and get a response.

Call format:

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

An XML response that I need to parse to verify success / failure. Additional information at http://sendgrid.com/documentation/ApiWebMail

What is the best way to make a call and parse the result?

+3
source share
2 answers

Ruby, Net:: HTTP-, , HTTPClient, . open-uri , .

HTML XML Nokogiri.

Ruby URL, URI, Addressable, a .

:

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

, Addressable:: URI :

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"}

, , uri.query_values=.

, HTTPClient, Ruby Open-URI , . URL-, Nokogiri XML:

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

XML- " , , :

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

, :

doc = Nokogiri::XML(returned_xml)

:

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

, , .

+5

. ? ( ) ? , CURL , . CURL Perl script. Python Ruby. Java .Net.

URL .

XML, .

0

Source: https://habr.com/ru/post/1790620/


All Articles