Quelqu'un peut-il donner un exemple pour savoir comment poster XML à l'aide HTTParty et Ruby on Rails?

StackOverflow https://stackoverflow.com/questions/3773939

  •  04-10-2019
  •  | 
  •  

Question

Je dois mettre un peu xml à un webservice et je suis en train d'utiliser HTTParty. Quelqu'un peut-il donner un exemple à la façon dont je vais sur le faire?

Voici le format du XML je besoin d'afficher:

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

Voici ma classe jusqu'à présent:

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

Je ne suis pas tout à fait sûr comment la chair sur add_candidate.

Toute aide serait appréciée.

Merci.

Était-ce utile?

La solution

Vous avez deux options. HTTParty vous permet d'afficher à la fois une chaîne ou un hachage.

La version chaîne serait:

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

fonctionnelle, mais pas assez. Je ferais ceci:

post('/test.xml', :body => {
  :Candidate => {
    :FirstName => first_name,
    :LastName  => last_name,
    :Email     => email,
    :Gender    => gender,
  }
}

Maintenant, je ne peux pas dire avec certitude si les espaces de noms sont requis par le point final, et si oui, si la version de hachage fonctionnera. Si tel est le cas, vous devrez peut-être aller avec faire le corps comme une chaîne.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top