kidbrax
kidbrax

Reputation: 2434

How can I post xml data using net/http that also has data in the url

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?

Upvotes: 1

Views: 3832

Answers (1)

David Barlow
David Barlow

Reputation: 4974

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.

Upvotes: 7

Related Questions