Simple API wrapper with HTTParty

673 Views Asked by At

I' am trying to create a simple wrapper for skyscanner API. The problems is that when try to get the sessionKey, what I get is <HTTParty::Response:0x10 parsed_response=nil, @response=#<Net::HTTPUnsupportedMediaType 415 Unsupported Media Type readbody=true>. I am not sure what is that I am doing wrong. I am new to rails and I will appreciate any direction on how to solve this problem?. Thanks

require 'httparty'

class Skyscanner
  include HTTParty
  format :json

  base_uri "http://partners.api.skyscanner.net/apiservices/pricing"


    def self.find(originplace, destinationplace)

        @options = { query:
             {
                :apiKey => "API_KEY",
                :country => "US",
                :currency => "USD",
                :locale => "en-us",
                :adults => 1,
                :children => 0,
                :infants => 0,
                :originplace => originplacea,
                :destinationplace => destinationplace,
                :outbounddate => "2017-02-20",
                :inbounddate => "2017-02-27",
                :locationschema => "iata",
                :cabinclass => "Economy"
            }
        }

        @headers = { 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => 'application/json'}

        @sessionkey_request = HTTParty.post("http://partners.api.skyscanner.net/apiservices/pricing/v1.0/",:body => @options,:headers => @headers)

        puts @sessionkey_request.inspect

        @get_sessionkey = @sessionkey_request.headers['location']
        @sessionkey = @get_sessionkey.to_s().split('/').last
        puts @sessionkey.inspect
    end

end

If anyone have a better way of approaching this wrapper, please advice me on how to. Thanks

0

There are 0 best solutions below