How to process POSTed XML through a Sinatra Ruby application

I plan to use Sinatra for the new tiny webservice (WS) that I need to build for the client.

WS will have only two methods: one is available through GET and one is through POST. For the POST method, the client sends an XML packet to the WS syntax, which will parse the data and give either a 200 HTTP HTTP response or a 40x error code.

My question is how to parse the incoming POSTed XML package in Sinatra?

Here is an example of what the following 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 any access to the data package through 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 the whole XML data packet that was POSTED for my WS?

+3
1

require 'rubygems'
require 'sinatra'

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

:

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

:

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

Source: https://habr.com/ru/post/1722619/


All Articles