0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.