Send Post request to external api using Rails and Net::HTTP


Solution#1 by Net::HTTP.post_form method


    #do post request
    response = Net::HTTP.post_form(URI.parse("https://buy.itunes.apple.com/verifyReceipt"), params)
    render json: response.body

Base64 Encode/Decode Link

In App Purchase Verification IOS link

Sending Post Request to external url/api Rails

Solution#2 by Net::HTTP.post_form method


 ##START ## XML Request body methods#######    
def xml_body_request_structure(hotels_club_city_id, locale='en')
  xml = ::Builder::XmlMarkup.new
  xml.instruct! :xml, version: "1.0"
  xml.OTA_HotelSearchRQ(xmlns: "http://www.opentravel.org/OTA/2015/05",
  'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
  'xsi:schemaLocation' => "http://www.opentravel.org/OTA/20015/05OTA_HotelAvailRQ.xsd",
  'EchoToken' => "HL", 'Target' => "Test", 'Version' => "1.003",
  'PrimaryLangID' => locale, 'MaxResponses' => "10000") do
    xml.POS {
        xml.Source { xml.RequestorID( 'ID' => "id is required", 'MessagePassword' => 'pwd here?' ) }
      }
      xml.Criteria( 'AvailableOnlyIndicator' => "true" ) do
       xml.Criterion do
        xml.HotelRef( 'HotelCityCode' => hotels_club_city_id )
        xml.TPA_Extensions{ xml.OrderBy( 'Value' => "rating" ) }
       end
      end
   end
   xml.target!.to_s
end
 ## END ##XML Request body methods#######

## START ##XML Post Request generating methods#######
def xml_post_request(request_body)
 timeout =  500
 uri = URI.parse(URI.encode(FEED_URL.strip))
# FEED_URL= url from which dev wants to fetch data
 http = Net::HTTP.new(uri.host, uri.port)
 #set https request 
 http.use_ssl = true
 #Post request initiated
 request = Net::HTTP::Post.new(uri.path, {'Content-Type' =>'application/x-www-form-urlencoded'})
 #request body with uri encoding for tags
 request.body = "OTA_request=#{URI.encode(request_body)}"
 #do request
 response = http.request(request)
 ###Parsing XML code###
 Nokogiri::XML(response.body) #Optional depend on developer whether she wants to use nokogiri?

end
## END ##XML Post Request generating methods#######


 ##City based properties fetching
def fetch_properties(city_id, locale='en')
  #hostelsclub api url
  request_body = xml_body_request_structure(city_id, locale)
  doc = xml_post_request(request_body)
  properties = doc.css('xmlns|Properties').first
  properties = properties.children if properties.present?
  return properties
end

The core of solution#2 is below


def xml_post_request(request_body)
        timeout =  500 #setting the time out limit
        uri = URI.parse(URI.encode(FEED_URL.strip)) 
# FEED_URL= url from which dev wants to fetch data
        http = Net::HTTP.new(uri.host, uri.port)
        #set https request 
        http.use_ssl = true #optional
        #Post request initiated
        request = Net::HTTP::Post.new(uri.path, {'Content-Type' =>'application/x-www-form-urlencoded'})
        #choose Content-Type of based on your own request demand
        #request body with uri encoding for tags
        request.body = "OTA_request=#{URI.encode(request_body)}" 
        #URI.encode is optional in my case it was required
        #do request
        response = http.request(request)
        ###Parsing XML code###
        Nokogiri::XML(response.body) #Optional depend on developer whether she wants to use nokogiri?
      end
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with: ,
Posted in APIs, Ruby Little Codes

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: