无效的内容类型:application / x-www-form-urlencoded,请求使用httparty

时间:2013-08-16 15:56:28

标签: ruby-on-rails ruby httparty

我正在使用httparty gem向intuit(商家帐户)发送帖子请求。

这是我的代码:

header = {"Content-Type" => "application/xml", 
          "content-length" => request.length 
         };

options = { :body => request,:options=>{ :headers => header} }
data = HTTParty.post(url,options) 

现在事件虽然我发送内容类型为application/xml但响应错误是:

 #<HTTParty::Response:0x44278 parsed_response="Invalid content type: application/x-www-form-urlencoded.\n", @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={"date"=>["Fri, 16 Aug 2013 15:49:50 GMT"], "server"=>["Apache"], "content-length"=>["57"], "connection"=>["close"], "content-type"=>["text/plain"]} 

我不知道我做错了什么。有什么想法吗?

这是data.request.inspect的输出:

#<HTTParty::Request:0x0000000440be98 @http_method=Net::HTTP::Post, @path=#<URI::HTTPS:0x00000bba0 URL:https://merchantaccount.ptc.quickbooks.com/j/AppGateway>, @options={:limit=>5, :default_params=>{}, :follow_redirects=>true, :parser=>HTTParty::Parser, :connection_adapter=>HTTParty::ConnectionAdapter, :body=>"<?xml version=\"1.0\" ?>\n<?qbmsxml version=\"2.0\"?>\n<QBMSXML>\n<SignonMsgsRq>\n<SignonAppCertRq>\n<ClientDateTime>\#{today}</ClientDateTime>\n<ApplicationLogin>\#{APPLICATION_LOGIN} </ApplicationLogin>\n<ConnectionTicket><\#{CONNECTION_TICKET}</ConnectionTicket>\n<Language>English</Language>\n<AppID>\#{APPLICATION_ID }</AppID>\n<AppVer>1</AppVer>\n</SignonAppCertRq>\n</SignonMsgsRq>\n</QBMSXML>", :options=>{:headers=>{"Content-Type"=>"application/xml", "standalone"=>"yes", "encoding"=>"UTF-8", "content-length"=>379}}}, @last_uri=#<URI::HTTPS:0x0000000440afe8 URL:https://merchantaccount.ptc.quickbooks.com/j/AppGateway>, @raw_request=#<Net::HTTP::Post POST>, @last_response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>>

1 个答案:

答案 0 :(得分:0)

应该是:

data = HTTParty.post(url, :body => request, :headers => {"Content-Type" => "application/xml"}) 

不要担心内容长度,这是HTTParty的工作。