English 中文(简体)
How to handle POSTed XML via Sinatra Ruby app
原标题:

I m planning on using Sinatra for a new tiny webservice (WS) that I need to put together for a client.

The WS will only have two methods, one accessed via GET and one via POST. For the POST method, the client will send an XML packet to the sinatra WS which will parse the data and issue either a 200 OK HTTP response or a 40x error code.

My question is how do I parse the incoming POSTed XML packet in Sinatra?

Here is an example of what the incoming data packet will look like:

<?xml version="1.0" encoding="utf-8" ?>
<Counts>
  <OccupiedCount>300</OccupiedCount>
  <ReservedCount>40</ReservedCount>
  <VacantCount>160</VacantCount>
  <TotalCount>500</TotalCount>
  <Checksum>0777d5c17d4066b82ab86dff8a46af6f</Checksum>
  <Timestamp>2009-11-21T14:06:19Z</Timestamp>
  <ApiKey>1234567890qwerty</ApiKey>
</Counts>

Is there someway to access the data packet via the Sinatra params object so that I can parse it with something like Crack XML? Or do I need to use some kind of Rack variable to get at the whole XML data packet that was POSTed to my WS?

问题回答

sinatra app

require  rubygems 
require  sinatra 

post  /form  do
    puts params[:xml]
end

Posting a request using your data:

curl -d "xml=<?xml version="1.0" encoding="utf-8" ?>
<Counts>
  <OccupiedCount>300</OccupiedCount>
  <ReservedCount>40</ReservedCount>
  <VacantCount>160</VacantCount>
  <TotalCount>500</TotalCount>
  <Checksum>0777d5c17d4066b82ab86dff8a46af6f</Checksum>
  <Timestamp>2009-11-21T14:06:19Z</Timestamp>
  <ApiKey>1234567890qwerty</ApiKey>
</Counts>
" http://localhost:4567/form

Result:

- - [11/Nov/2009:12:05:40 PST] "POST /form HTTP/1.1" 200 0
- -> /form
<?xml version=1.0 encoding=utf-8 ?>
<Counts>
  <OccupiedCount>300</OccupiedCount>
  <ReservedCount>40</ReservedCount>
  <VacantCount>160</VacantCount>
  <TotalCount>500</TotalCount>
  <Checksum>0777d5c17d4066b82ab86dff8a46af6f</Checksum>
  <Timestamp>2009-11-21T14:06:19Z</Timestamp>
  <ApiKey>1234567890qwerty</ApiKey>
</Counts>




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签