在响应文本中显示换行符

在响应文本中显示换行符

本文介绍了在响应文本中显示换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Swagger UI v2.2.0.我有一个返回纯文本的 RESTful 方法.我想用换行符显示此文本.

I use Swagger UI v2.2.0. I have a RESTful method which returns plain text. I want to display this text with line breaks.

目前返回的文本包含换行符,但显示为\n.Content-Type 响应头是 text/plain.

At the moment, the returned text contains new line characters, but they are displayed as \n. The Content-Type response header is text/plain.

我可以返回插入了新行字符(例如,<br> 标签)的其他内容的文本.我也可以更改 Content-Type.我只需要显示文本中的实际换行符.

I can return the text with something else inserted of new line characters (e.g., <br> tags). I also can change the Content-Type. I just need actual line breaks in the displayed text.

有没有办法做到这一点?

Is there a way to achieve this?

推荐答案

不确定是否有帮助,但我也遇到了同样的问题.

Not sure if it helps, but I also faced the same issue.

我想在 nodejs 服务器中执行以下代码:

I wanted to execute the following code in nodejs server:

app.post('/xyz', function (req, res) {
    res.status(400).send("MyError\nMyErrorStack:\nStackLine1\nStackline2")

}

和想要的输出如:

"MyError
MyErrorStack:
StackLine1
Stackline2"

但是得到:

"MyError\nMyErrorStack:\nStackLine1\nStackline2"

"MyError\nMyErrorStack:\nStackLine1\nStackline2"

所以我将字符串拆分为数组,如下所示:

so instead i split the string into array as shown used following:

err.error = errObj.stack.split("\n")

app.post('/xyz', function (req, res) {
    res.status(400).send(err)

}

打印出来

"Error": {
    "error": [
      "Error: ENOENT: no such file or directory, open 'c:\\....'",
      "    at Error (native)",
      "    at Object.fs.openSync (fs.js:584:18)",
      "    at Object.fs.writeFileSync (fs.js:1224:33)",
      "    at Object.fs.appendFileSync (fs.js:1283:6)",
      "    at ...",
      "    at ...",
      "    at ...",
      "    at ...",
      "    at ...",
      "    at ..."
    ]
}

这对我有用.

这篇关于在响应文本中显示换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 15:28