cman77
cman77

Reputation: 1783

Rails API Create Action accept JSON OR XML

I would like to my create action on my rails API to accept either a JSON POST or and XML POST. Do I need to do anything special or should it just work out of the box as long as every comes through as params?

Upvotes: 0

Views: 664

Answers (2)

Trevor Elwell
Trevor Elwell

Reputation: 189

Be careful, Rails 4 removed support for XML. You may need to install the actionpack-xml_parser gem to support receiving XML as POST params. This requires you to add the following to config/application.rb

config.middleware.insert_after ActionDispatch::ParamsParser, ActionDispatch::XmlParamsParser

This was answered originally here.

Don't forget to restart your Rails server once you're done :)

Upvotes: 1

Austio
Austio

Reputation: 6085

Rails just sees them as params that are passed in. However you will want to have a respond to block that responds properly to xml vs json

respond_to do |format|
  format.xml { #render XML STUFF }
  format.json { #render JSON STUFF }
end

http://api.rubyonrails.org/classes/ActionController/MimeResponds.html

Upvotes: 1

Related Questions