使用 Deno.Command 将引号变成反斜杠

问题描述 投票:0回答:2
await new Deno.Command('cmd', {
    args: [
        '/c',
        'start',
        `https://accounts.spotify.com/authorize?${new URLSearchParams({
            client_id,
            response_type: 'code',
            redirect_uri: 'http://localhost:8080/callback',
            scope: 'user-library-read',
        })}`.replaceAll(/&/g, '"&"'),
    ],
    stdout: 'piped',
    stderr: 'piped',
}).output();

我正在尝试在 Windows 上的 Deno 浏览器中打开此 URL。然而,打开的网址是这样的:

https://accounts.spotify.com/authorize?client_id=x\&\response_type=code\&\redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fcallback\&\scope=user-library-read

其中

"
已替换为
\
。如果我不将 URL 参数中的
&
用引号括起来,那么浏览器中只会打开一个 URL 参数,如下所示:

https://accounts.spotify.com/authorize?client_id=x

我想在浏览器中打开包含所有参数的 URL,但我不知道如何操作。

typescript windows cmd spotify deno
2个回答
0
投票

根据超级用户的这个答案,带有特殊字符的URL应该位于另一个(可能为空)参数之后。试试这个:

await new Deno.Command('cmd', {
    args: [
        '/c',
        'start',
        '',
        `https://accounts.spotify.com/authorize?${new URLSearchParams({
            client_id,
            response_type: 'code',
            redirect_uri: 'http://localhost:8080/callback',
            scope: 'user-library-read',
        })}`,
    ],
    stdout: 'piped',
    stderr: 'piped',
}).output();

© www.soinside.com 2019 - 2024. All rights reserved.