ruby-on-railsrubyruby-grapegrape-api

How to return text with line break with Grape API?


I am trying to create an endpoint where the response body is just a string with line breaks, but the response keeps showing the \n character.

My endpoint code:

get '' do
  header('Content-Type', 'text/plain')
  body("Hello\nWorld")
end

And this is the response I see in Postman:

enter image description here

What am I missing here?

Thank you


Solution

  • I was able to make it work like I wanted with the following code:

    get '' do
      env['api.format'] = :binary
      header('Content-Type', 'text/plain')
      body("Hello\nWorld")
    end
    

    enter image description here