多行字符串作为参数

huangapple go评论111阅读模式
英文:

Multi-Line Strings As Parameters

问题

Express.js代码接受参数的部分:

app.get('/:prompt', async (req, res) => {
  const prompt = req.query.prompt;
  // 使用prompt进行某些操作并发送响应
}

客户端JavaScript代码试图发送多行字符串的部分:

export function btn_click() {
    console.log('Clicked')
    const prompt = $w("#textBox1").value
    // console.log(typeof prompt);
    const Orignal_URL = 'https://11bf-2401-4900-1f35-97fc-68a4-6e62-bb93-34d5.in.ngrok.io/';
    const URL = Orignal_URL.concat(prompt)
    console.log(URL);

    fetch(URL, { method: 'get' })
        .then(res => res.json())
        .then(ans => {
            console.log(ans);
            const blank = $w("#text3");
            if (ans['Answer'] === undefined){
                blank.text = '请提供所需的信息';
            }
            else {
                blank.text = String(ans['Answer']);
            }
            blank.show()
        });
}

当我发送多行字符串作为参数时,变量"prompt"等于"undefined"。如何解决这个问题?

英文:

How can you pass multi-line strings (with spaces and tabs) as a parameter to an express server?

Express.js code that accepts the parameter :

app.get('/:prompt', async (req, res) => {
  const prompt = req.query.prompt;
  // Do something with the prompt and send a response
}

Client-Side Javascript code that tries to send a multi-line string :

export function btn_click() {
    console.log('Clicked')
    const prompt = $w("#textBox1").value
    // console.log(typeof prompt);
    const Orignal_URL = 'https://11bf-2401-4900-1f35-97fc-68a4-6e62-bb93-34d5.in.ngrok.io/';
    const URL = Orignal_URL.concat(prompt)
    console.log(URL);

    fetch(URL, { method: 'get' })
        .then(res => res.json())
        .then(ans => {
            console.log(ans);
            const blank = $w("#text3");
            if (ans['Answer'] === undefined){
                blank.text = 'Please enter the required information';
            }
            else {
                blank.text = String(ans['Answer']);
            }
            blank.show()
        });
}

When I send a multi-line string as the parameter, the variable "prompt" is equal to "undefined". How do I solve this?

Update: Added the code that sends a request with the string for more clarity

答案1

得分: 2

一个URL不能包含换行符。HTTP资源名称必须是单行字符串,也不应包含空格。要解决这个限制,您需要通过encodeURIComponent对提示参数进行URL编码

const url = `https://example.org/app/${ encodeURIComponent(prompt) }`;

要在Express.js中获取这个值,您可以使用路由参数,并通过req.params自动解码它。请注意,您需要使用(*)允许参数中的斜杠

app.get('/app/:prompt(*)', async (req, res) => {
  const prompt = req.params.prompt;
  // 对提示执行某些操作并发送响应
});

或者,您可以(并且可能应该)使用URL查询字符串参数发送该值:

const url = `https://example.org/app?prompt=${ encodeURIComponent(prompt) }`;

在Express.js中,您可以使用req.query(正如您所做的那样)来访问它:

app.get('/app', async (req, res) => {
  const prompt = req.query.prompt;
  // 对提示执行某些操作并发送响应
});
英文:

An URL cannot contain newlines. A HTTP resource name must be a single-line string and should not contain spaces either.

To work around this limitation, you need to URL encode the prompt parameter via encodeURIComponent:

const url = `https://example.org/app/${ encodeURIComponent(prompt) }`;

To get this value in Express.js, you can use a route parameter and access it via req.params which will automatically decode it. Notice that you need to use (*) to allow slashes in the parameter:

app.get('/app/:prompt(*)', async (req, res) => {
  const prompt = req.params.prompt;
  // Do something with the prompt and send a response
});

Alternatively, you can (and probably should) send the value using an URL query string parameter:

const url = `https://example.org/app?prompt=${ encodeURIComponent(prompt) }`;

In Express.js, you use req.query (as you did) to access it:

app.get('/app', async (req, res) => {
  const prompt = req.query.prompt;
  // Do something with the prompt and send a response
});

huangapple
  • 本文由 发表于 2023年1月3日 21:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/74993719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定