简体   繁体   English

如何在 REST Client for VS Code 中输入多行字符串?

[英]How to enter multi-line strings in REST Client for VS Code?

EDIT There's some confusion about what I am storing.编辑我存储的内容有些混乱。 I am making a code snippet web app in Express.js and Mongodb.我正在 Express.js 和 Mongodb 中制作代码片段 web 应用程序。 I am not storing code to execute it later, I am storing it, and then displaying it in plain-text on the web app, and will allow the user to copy the code to use themselves.我不是存储代码以便以后执行它,而是存储它,然后在 web 应用程序上以纯文本形式显示它,并允许用户复制代码以供自己使用。 I have a syntax highlighter module installed to make it look good, but the code itself is not executed.我安装了一个语法高亮模块以使其看起来不错,但代码本身并未执行。

It is similar to GitHub Gists.它类似于 GitHub Gists。

Another Edit How can I store multi-line text in Mongodb, then when fetched from server, convert it automatically to use \n for line breaks, and \t for tabs?另一个编辑我如何在 Mongodb 中存储多行文本,然后从服务器获取时,自动将其转换为使用 \n 换行符和 \t 制表符?

I am making a REST API in Express.js.我正在 Express.js 中制作 REST API。 I am using Rest Client extension in VS Code to test out the API.我在 VS Code 中使用 Rest 客户端扩展来测试 API。

I am making a post request to the server to create a code snippet.我正在向服务器发出发布请求以创建代码片段。 The fields for the snippet model are all strings, but I need one field to hold actual code inside it.片段 model 的字段都是字符串,但我需要一个字段来保存其中的实际代码。 I tried using backticks and a few other things, but cannot seem to figure it out.我尝试使用反引号和其他一些东西,但似乎无法弄清楚。

Here's my file "api.rest"这是我的文件“api.rest”

@hostname = localhost
@port = 5000
@host = {{hostname}}:{{port}}
@contentType = application/json

# POST /users
POST http://{{host}}/snippets HTTP/1.1
Content-Type: {{contentType}}

{
    "title": "React Component",
    "code": "CHANGE THIS TO A MULTI-LINE JAVASCRIPT CODE SNIPPET",
    "creator": "nero1333"
}

Your use case is very simple.您的用例非常简单。 You just want to save text input in the database and fetch it later to display on the client-side.您只想将文本输入保存在数据库中并稍后获取它以显示在客户端。

You just have to send the code in string form containing \n for a new line, \t for tabs, and \" to escape the double quotes used in the code.您只需以字符串形式发送代码,其中包含\n用于换行, \t用于制表符, \"以转义代码中使用的双引号。

If you are taking input from a text editor using a webpage, that editor will escape these values for you and you will send it to the server to store.如果您使用网页从文本编辑器获取输入,该编辑器将为您转义这些值,然后您将其发送到服务器进行存储。

For example:例如:

You want to save this code as you mentioned in the comment:您希望按照评论中提到的方式保存此代码:

import React from 'react'
class className extends React.Component
    render() {
        <div></div>
    }

Send it like:像这样发送:

{
    "code": "import React from 'react'\nclass className extends React.Component\n\trender() {\n\t\t<div></div>\n\t}"
}

Same way you will enter it in the REST client to test the API.同样的方法,您将在 REST 客户端中输入它来测试 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM