Can someone provide an example for how to post XML using HTTParty and Ruby on Rails? -
i need post xml webservice , i'm trying use httparty. can provide example how go doing so?
here format of xml need post:
<candidate xmlns="com.mysite/2010/10/10" xmlns:i="http://www.w3.org/2001/xmlschema-instance"> <firstname></firstname> <lastname></lastname> <email></email> <gender></gender> </candidate>
here class far:
require 'httparty' class webservice include httparty format :xml base_uri 'mysite.com' default_params :authorization => 'xxxxxxx' def self.add_candidate(first_name,last_name,email,gender) post('/test.xml', :body => "") end end
i'm not quite sure how flesh out add_candidate.
any appreciated.
thanks.
you've got 2 options. httparty allows post both string or hash.
the string version be:
post('/test.xml', :body => "<candidate xmlns=\"com.mysite/2010/10/10\" xmlns:i=\"http://www.w3.org/2001/xmlschema-instance\"><firstname>#{first_name}</firstname><lastname>#{last_name}</lastname><email>#{email}</email><gender>#{gender}</gender></candidate>")
functional, not pretty. i'd instead:
post('/test.xml', :body => { :candidate => { :firstname => first_name, :lastname => last_name, :email => email, :gender => gender, } }
now, can't sure whether namespaces required endpoint, , if so, whether hash version work. if that's case, may have go doing body string.
Comments
Post a Comment