如何使用net/http发布xml数据,网址中也包含数据

kid*_*rax 1 xml post ruby-on-rails http net-http

我正在使用api,它要求我将xml发布到url,例如someapi.com?userID = 123.到目前为止,我已经尝试过这个(假设xml已经在xml变量中编写):

url = URI.parse('http://www.someapi.com/process_leads.asp')
request = Net::HTTP::Post.new(url.path)
request.content_type = 'text/xml'
request.body = xml
request.set_form_data({'userID' => '1204'}, ';')
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
Run Code Online (Sandbox Code Playgroud)

我想弄清楚我是否可以将userID作为表单数据但是也可以发布xml?我基本上应该将xml发布到http://www.someapi.com/process_leads.asp?userID=1204.那可能吗?

Dav*_*low 7

我会考虑使用Http库,例如HTTParty

使用HTTParty为您的请求的示例如下所示:

HTTParty.post('http://www.someapi.com/process_leads.asp', :query => {:userID => 1024}, :body => xml )
Run Code Online (Sandbox Code Playgroud)

:query选项获取将添加到post URL的键/值的哈希值:body是xml的位置.

注意:有些api要求xml有一个名字,例如你可能需要做类似的事情

:body => "request=#{xml}"
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.