Tyrael Tong
Tyrael Tong

Reputation: 761

How to get xml post request parameters in Sinatra?

I have a very simple Sinatra app which only does log out the params in the action, and then I use curl to send post data in xml format, but sinatra didn't get the xml parsed:

echo '<something>tyrael tong</something>' | curl -X POST -H 'Content-Type: text/xml' -d @- http://localhost:9528/status/update

I searched through google with no solution to this. Am I suppose to parse the xml post data by myself?

Upvotes: 3

Views: 2746

Answers (4)

Tyrael Tong
Tyrael Tong

Reputation: 761

Find a rack parser: https://github.com/achiu/rack-parser which could do the job I want: parse the post body into parameter.

P.S. And need to set the content type to "application/xml"

Upvotes: 3

djd
djd

Reputation: 1001

try this code require 'plist'

post 'some/route'
 content_type :xml
 value=Plist::parse_xml(request.body)
end

Upvotes: 3

Moiz Raja
Moiz Raja

Reputation: 5760

As the body of your post message is an xml it will not be parsed by either rails/sinatra. If you want parsing to be done you will need to change the format of data that you are sending and set the content type to "application/x-www-form-urlencoded". Then Sinatra will parse that data and put it into the params hash.

Upvotes: 0

Slartibartfast
Slartibartfast

Reputation: 8805

Yes, content type is just a hint for a server how to handle it. If your server can receive say XML or JSON, content type can tell you how to parse it.

Upvotes: 2

Related Questions