1

I am working with an api that requires me to post xml to url such as someapi.com?userID=123. Thus far, I have tried this (assume the xml is composed already in the xml variable):

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)}

I am trying to figure out if I can have the userID as form data but also post xml? I am basically supposed to post the xml to http://www.someapi.com/process_leads.asp?userID=1204. Is that possible?

kidbrax
  • 2,364
  • 3
  • 30
  • 38
  • Should I be using multipart, such as http://stackoverflow.com/questions/184178/ruby-how-to-post-a-file-via-http-as-multipart-form-data – kidbrax Nov 03 '11 at 03:02

1 Answers1

7

I would consider using a Http library, e.g. HTTParty

Example using HTTParty for your request would look something like:

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

the :query option takes a hash of key/values which will be added to the post URL, the :body is where the xml goes.

NOTE: some api's require the xml to have a name e.g. you may have to do something like

:body => "request=#{xml}"

Hope this helps.

David Barlow
  • 4,914
  • 1
  • 28
  • 24