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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I was afraid I'd have to move to a http library. I'll give this a shot.
Don't be afraid... It will make your life easier, and your code smell less.
Worked perfectly after I banged my head against the wall for hours only to realize I had a simple typo!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.