简体   繁体   中英

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. 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. 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.

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?

I am making a REST API in Express.js. I am using Rest Client extension in VS Code to test out the 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. I tried using backticks and a few other things, but cannot seem to figure it out.

Here's my file "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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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