Quick Tip: Set response content type

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.

vinothini balak...
Thanks a lot for your tips.It

Thanks a lot for your tips.

It will be useful while making ASyncHttp call.

Thanks

Vinothini B

Spritle Software

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


DecisionPoint K...
Actually, this wouldn't be

Actually, this wouldn't be useful for AsyncHttp, because this is for the headers you are returning to the browser (the mobile device). AsyncHttp has its own methods to set the content type and accepted content type.

Vote: 
Vote up!
Vote down!

Points: 0

You voted ‘up’


Log in to post comments