How to return text with line break with Grape API?

321 Views Asked by At

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

2

There are 2 best solutions below

0
joaopribs On BEST ANSWER

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

0
Karol Ostrowski On

\n is a new line character for UNIX systems, and in RFC2046 is specified that new line characters in text/plain format should use \r\n (Windows style new line).

So In Your case server should return "Hello\r\nWorld"

For more verbose answer check this one here