2

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

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    "*When I send a multi-line string as the parameter*" - how do you do that? – Bergi Jan 03 '23 at 13:09
  • Please you us an example. You can read [example] – Andres Gardiol Jan 03 '23 at 13:22
  • @Bergi I have updated the question with the function that gets called to send a request with the multi-line string, however as mentioned before the final variable "prompt" has the value "undefined" when I try to pass it this way. – Aviral Dhingra Jan 03 '23 at 13:28
  • 3
    URL-encode it?. – Dave Newton Jan 03 '23 at 13:29
  • @AndresGardiol I cannot give you an exact example as my client-side javascript is actually being run on editor x but I can show you how to reproduce it... I made the client-side script console.log the "URL" variable and then copy-pasted it into the browser. The URL was shorterned to : "https://11bf-2401-4900-1f35-97fc-68a4-6e62-bb93-34d5.in.ngrok.io/if%200%20==%200%20print(%22Success%22)", the result is the same. – Aviral Dhingra Jan 03 '23 at 13:32
  • @DaveNewton I tried doing that as well (as mentioned in the previous comment regarding the example), however, the value of the variable is still coming out to be undefined. – Aviral Dhingra Jan 03 '23 at 13:37
  • @Bergi Do you think it's possible and efficient to pass the multi-line string using the post method instead of as a parameter? If yes, how do I parse the string to contain backslash commands that javascript supports once I receive it? – Aviral Dhingra Jan 03 '23 at 14:46
  • @AviralDhingra Possible, yes; efficient, not more than via a query string, though it leads to cleaner urls. But both should work. – Bergi Jan 03 '23 at 15:01

1 Answers1

2

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
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375