如何使用Rails上的HTTParty使用XML数据主体发出POST请求

时间:2016-05-29 08:48:05

标签: ruby-on-rails ruby xml request httparty

我目前正在尝试对服务器进行api调用,该服务器只接受xml形式的数据。 我尝试从邮递员那里调用服务,它就像一个魅力。

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"

现在我尝试使用以下代码在Ruby on Rails中实现这个api调用,但遗憾的是我每次都会收到一个ERROR 500.

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

你有任何提示让这项工作吗?

2 个答案:

答案 0 :(得分:2)

我假设500错误代码来自您的控制器,而不是来自API调用。如果您检查日志文件,您将看到更多详细信息。我在猜测:

您正在将HTTP响应发送给render,但要求它将其呈现为XML。

尝试

render xml: response.body

代替。

答案 1 :(得分:0)

也许将instruct标签放在身体顶部可以正常工作,

:body => '<?xml version="1.0" encoding="UTF-8"?><Request LanguageCode="en"...

希望有所帮助