Roshnal Roshnal - 6 months ago 45
Ruby Question

How do you make POST request with a XML data body using HTTParty on Rails

I'm currently trying to make an api call to a server which just accepts my data in form of an xml.
I tried calling the service from postman and it works like a charm.

curl -v -X POST -H "x-apiKey: 83202136355d1704350fb3ef596ac126" -H "Content-Type: application/xml" -d '<Request LanguageCode="en" emailAddress="your email" mobileNumber="+4477....." gate="212" boardingHHMM="18:40" departHHMM="19:10" arriveHHMM="20:30" ffMiles="" DepartTerminal="Term 2" ffTier="" message="Sample" CabinName="Economy" > <Barcode firstName="Arie" lastName="van der Veek" title="MR" bookingRef="PNR123" depAirportCode="AMS" arrAirportCode="CDG" carrier="XS" flightNumber="56" depDate="2015-06-06" classCode="Y" seatNumber="21B" seqNumber="002" ffAirline="ZZ" ffNumber="123456789" ticketNumber="000123456789012" issuingCarrier="ZZ" /> </Request>' "https://dev2dbp.api.aero/api/ZZ"


Now I tried to implement this api call in Ruby on Rails using the following code, but sadly I'm getting an ERROR 500 everytime.

class FlightTicketController < ApplicationController

# get 'flight_ticket/get_ticket'
def get_ticket
response = HTTParty.post(
"https://dev2dbp.api.aero/api/ZZ",
:headers => {"x-apiKey" => "83202136355d1704350fb3ef596ac126","Content-Type" => "application/xml"},
:body => '<Request LanguageCode="en" emailAddress="your email" mobileNumber="+4477....." gate="212" boardingHHMM="18:40" departHHMM="19:10" arriveHHMM="20:30" ffMiles="" DepartTerminal="Term 2" ffTier="" message="Sample" CabinName="Economy" > <Barcode firstName="Arie" lastName="van der Veek" title="MR" bookingRef="PNR123" depAirportCode="AMS" arrAirportCode="CDG" carrier="XS" flightNumber="56" depDate="2015-06-06" classCode="Y" seatNumber="21B" seqNumber="002" ffAirline="ZZ" ffNumber="123456789" ticketNumber="000123456789012" issuingCarrier="ZZ" /> </Request>'
)

render xml: response
end


end


Do you have any tips to make this work?

Answer

I assume that the 500 error code is from your controller and not from the API call. If you check the log files you will see more details. I'm taking a guess:

You are handing a HTTP Response to render but ask it to render it as XML.

Try

render xml: response.body

instead.