Home > Blockchain >  Rails controller render json without encoding
Rails controller render json without encoding

Time:06-26

I am trying to send json data from my rails controller but it always converts & into unicode \u0026. Is it possible to tell rails controller not to convert & into unicode \u0026 ?

Below is how my method looks like in controller (this is just dummy test method)

def send_detail
  render json: { data: "name?surname&otherdetail" } 
end

when I visit localhost:3000/tests/send_detail in postman, I get { "data":"name?surname\u0026otherdetail" }

How can I tell render method not to convert & into unicode character \u0026 ?. If possible I would like to preserve & only for this method rather than changing Rails config to not covert json data in unicode character for all rails application.

UPDATE

I am able to force rails not to covert & into unicode character by adding this line ActiveSupport.escape_html_entities_in_json = false but this changes the settings in all Rails application, which is not what I want. I only like to preserve & in one method.

CodePudding user response:

You can manually convert it in the single action that you want it not to apply:

def send_detail
  render json: JSON.generate({ data: "name?surname&otherdetail" })
end

  • Related