views:

660

answers:

1

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?

A: 

sinatra app

` require 'rubygems' require 'sinatra'

post '/form' do puts params[:xml] end

`

Posting a request using your data

Blockquote curl -d "xml= 300 40 160 500 0777d5c17d4066b82ab86dff8a46af6f 2009-11-21T14:06:19Z 1234567890qwerty " http://localhost:4567/form

Result - - [11/Nov/2009:12:05:40 PST] "POST /form HTTP/1.1" 200 0 - -> /form 300 40 160 500 0777d5c17d4066b82ab86dff8a46af6f 2009-11-21T14:06:19Z 1234567890qwerty

Devender Gollapally
Don't know why the xml data gets garbled but it definitely works on my machine
Devender Gollapally
the data isn't coming in as an XML= param though. its just the whole data packet, not a packet that starts with "XML=<xml data here />" so params[:xml] doesn't exist in this case
cpjolicoeur
it doesn't seem that request.body works as its not a string object, and request.POST is a hash, not the whole object
cpjolicoeur
I found the answer. I can access request.body.readto get the data packet
cpjolicoeur