844
Views
7
Comments
Solved
REST API Text Response with "\n"
Question

Hi,

My RESTful web service is returning a simple text response with "\n" included within the text. But when the response reach to the end point, an extra back slash is added to all "\n" and became "\\n". Please advise am I doing anything wrong or is this not a correct way of doing thing. 

Response captured using debugger at service studio



Response captured at Postman


From Integration monitor


Thank for viewing.


Regards,

Ye Yint

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Ye,

You can use Chr(10) instead of NewLine(). This should produce a \n only.

EDIT: 13 -> 10

UserImage.jpg
virat kohli

Kilian Hekhuis wrote:

Hi Ye,

You can use Chr(10) instead of NewLine(). This should produce a \n only.

EDIT: 13 -> 10

Hi kilian,

I want a new line. So what should i use? i tried newline(), it gives \r\n and then i tried chr(10) it gives \n. I am exposing rest api and i want to send error where i need to break a message. I am working on reactive web application.

Regards,

Virat


2019-09-24 18-41-25
Jorge Martins
 
MVP

Hi Ye Yint,

In C and similar-syntax languages, you represent control characters like newline (or line-feed) or tab with an initial '\' followed by some extra character to represent an "escape sequence" (e.g. '\n' for newline and '\t' for tab).

When writing these character literals in a string in those languages you'd use something like:

"First line\nSecond line\n\tThird indented line"

where the compiler would know to interpret the "\n" characters as a change of line, and the "\t" characters as a tab. This means that if you want to actually include the two characters on your string (not the control character) you need to add an extra '\' there... "\\" is how you represent the '\' character in those languages.

From what I see in your screenshots, it seems you are implementing your own REST API method and you're using "\n" in your output string assuming it behaves like in those common languages... when you should be using the NewLine() function to return the newline character instead.

Hope this helps

2018-03-09 01-38-37
Ye Yint

Hi Jorge,


Thank You for the reply. I tried using "newline()" also but when I use the newline, it produces "\r\n" instead of "\n" only. Is there a way to produce only "\n".


Regards,

Ye Yint

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP
Solution

Hi Ye,

You can use Chr(10) instead of NewLine(). This should produce a \n only.

EDIT: 13 -> 10

UserImage.jpg
virat kohli

Kilian Hekhuis wrote:

Hi Ye,

You can use Chr(10) instead of NewLine(). This should produce a \n only.

EDIT: 13 -> 10

Hi kilian,

I want a new line. So what should i use? i tried newline(), it gives \r\n and then i tried chr(10) it gives \n. I am exposing rest api and i want to send error where i need to break a message. I am working on reactive web application.

Regards,

Virat


2018-03-09 01-38-37
Ye Yint

Hi Kilian,


Thank you for the help and reply. I put Chr(10) and it produces exactly as "\n". 

Regards,

Ye Yint

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

I'm glad it worked :). Happy coding!

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Virat,

I'm not sure what you mean. If Chr(10) works, what's your problem?

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.