2 Replies Latest reply on May 20, 2013 8:21 PM by DecisionPoint Keith Gable

    Quick Tip: Set response content type

    DecisionPoint Keith Gable

      I'm surprised this doesn't come up more often. By default, when you return something from a controller, it returns it with the content type of "text/html". This works well when your controller actions return HTML, or you tell jQuery to interpret the response as something else (JSON, XML), but if you ever want to return the correct content type, that's possible too. For example, I am using Advanced REST Client in Chrome to test some of my Rho controllers, and it can format the response properly if the correct content type is returned. Also, jQuery can magically work too.

       

      So, how do you do it?

       

      Well, before you render, do this:

       

      @response["headers"]["Content-Type"] = "application/json; charset=utf-8"
      

       

      or

       

      @response["headers"]["Content-Type"] = "application/xml; charset=utf-8"
      

       

      This is similar to handling a JSON or XML format in Rails.