【问题标题】:How to GET request with "#" character in URL directly by node.js?如何通过node.js直接在URL中使用“#”字符获取请求?
【发布时间】:2021-08-09 08:59:34
【问题描述】:

我知道它是有线的,但是服务器端已经实现了没有 URI 编码/解码功能,并且无法修复。

所以我应该在 GET 查询中使用 URL 中的字符,没有任何编码。

当我请求 URL 时:

https://example.com/check?id=myAccount&password=qwerty!@#

然后http(s)模块自动改为

https://example.com/check?id=myAccount&password=qwerty!@

因此,我无法按我的意愿发送数据。没有其他 NPM 模块怎么办?

我也检查了文档,但找不到答案。

https://nodejs.org/api/http.html#http_http_request_url_options_callback

我在 Node.js 上使用了以下代码。

// Example : https://example.com/check?id=myAccount&password=qwerty!@#
(_url, params) => {
    return new Promise((resolve, reject) => {
        if (params) {
            let queryParams = `?`;
            for (let key in params) {
                const value = params[key];
                queryParams += `${key}=${value}&`;
            }
            queryParams = queryParams.slice(0, -1);
            _url += queryParams;
        }


        const options = {
            method: `GET`,
        }

        const req = https.request(_url, options, (res) => {
            // When I checked it, request URL is changed to https://example.com/check?id=myAccount&password=qwerty!@
            // (Removed "#" automatically)
            // console.log(req)

            let data = ``;
            res.on(`data`, (d) => {
                data += d.toString();
                process.stdout.write(d);
            });


            res.on(`end`, () => {
                // process.stdout.write(d);
                resolve(data);
            });
        });

        req.on(`error`, (e) => {
            console.error(e);
        });
        req.end();

    });
}

【问题讨论】:

    标签: node.js http


    【解决方案1】:

    您无法访问它,因为它永远不会到达您的服务器。 Http 客户端(浏览器或 curl)不发送片段(URL 的 # 部分)。

    因此,如果您向http://test.com/path#myCustomData 发出请求,则发送的只有http://test.com/path

    https://stackoverflow.com/a/9967728/5521670

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 2016-05-03
      • 2013-08-21
      • 2013-07-23
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多