Displaying line breaks in the response text

2019-03-02 04:58发布

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.

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

enter image description here

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?

2条回答
我想做一个坏孩纸
2楼-- · 2019-03-02 05:18

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

I wanted to execute the following code in nodejs server:

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

}

and wanted output like:

"MyError
MyErrorStack:
StackLine1
Stackline2"

but got :

"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)

}

and this printed

"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 ..."
    ]
}

This worked for me.

查看更多
干净又极端
3楼-- · 2019-03-02 05:33

This is currently not possible due to a documented bug in Swagger UI. Reference:

Inconsistent Markdown Newlines #2981

Bug in Model (Definition) Description with newline characters #3078

The second Issue listed, #3078 contains some discussion on overriding the styles used to render that part of the UI, but results appear inconsistent.

Note: I have subscribed to those issues and will update the answer and/or flag to close as no longer relevant when it is resolved.

查看更多
登录 后发表回答