首页 \ 问答 \ Node.js - 带cookie的多个请求(Node.js - multiple requests with cookies)

Node.js - 带cookie的多个请求(Node.js - multiple requests with cookies)

我正在使用Node.js构建一个应用程序。 我正在使用request包来发出服务器端GET请求。 具体来说,我正在发出使用自定义HTTP标头的请求:

https://www.npmjs.com/package/request#custom-http-headers

该文档显示了如何一次发出一个请求。 但是,我需要向两个不同的API发出请求。 有谁知道如何做到这一点? 我目前提出一个请求的代码:

var cookie = parseCookie.parseCookie(req.headers.cookie);
var cookieText = 'sid='+cookie;
var context;

function callback(error, response, body) {

    var users = JSON.parse(body);
    res.render('../views/users', {
        context: users
    });
}

var options = {
    url: 'http://localhost:3000/api/admin/users/',
    headers: {
        host: 'localhost:3000',
        connection: 'close',
        cookie: cookieText
    }
};

request(options, callback);
//need to make a request to another API.

作为旁注,我需要使用自定义HTTP标头的原因是我可以包含一个cookie,以便我的API可以进行身份​​验证。


I am building an app using Node.js. I am using the request package to make server-side GET requests. Specifically, I am making requests that use custom HTTP headers:

https://www.npmjs.com/package/request#custom-http-headers

The documentation shows how to make one request at a time. However, I need to make a request to two different API's. Does anyone have any idea how to do this? My current code for making one request:

var cookie = parseCookie.parseCookie(req.headers.cookie);
var cookieText = 'sid='+cookie;
var context;

function callback(error, response, body) {

    var users = JSON.parse(body);
    res.render('../views/users', {
        context: users
    });
}

var options = {
    url: 'http://localhost:3000/api/admin/users/',
    headers: {
        host: 'localhost:3000',
        connection: 'close',
        cookie: cookieText
    }
};

request(options, callback);
//need to make a request to another API.

As a side note, the reason I need to use custom HTTP headers is so I can include a cookie so my API can authenticate.


原文:https://stackoverflow.com/questions/34958018
更新时间:2022-07-28 09:07

最满意答案

Eric的另一个答案是错误的。 他提到的命名空间声明与这个问题无关。

它不起作用的真正原因是由于安全问题 (参见第4197 号 问题 , 第111905号 )。

想象这种情况:

  1. 您收到一封来自攻击者的电子邮件,其中包含您下载的附件的网页。

  2. 您可以在浏览器中打开现在的本地网页。

  3. 本地网页创建一个<iframe>其源代码为https://mail.google.com/mail/ 。

  4. 由于您已登录Gmail,该框架会将邮件加载到收件箱中。

  5. 本地网页通过使用JavaScript来访问frames[0].document.documentElement.innerHTML来读取帧的内容。 (在线网页将无法执行此步骤,因为它来自非Gmail来源;同源策略会导致读取失败。)

  6. 本地网页将您的收件箱的内容放入<textarea>并通过表单POST将数据提交给攻击者的Web服务器。 现在攻击者有你的收件箱 ,这可能对垃圾邮件或识别盗窃是有用的。

通过使用Chrome打开的本地文件进行限制, Chrome可以覆盖上述情况。 为了克服这些限制,我们有两个解决方案:

  1. 尝试使用--allow-file-access-from-files标志运行Chrome。 我没有自己测试,但如果它有效,您的系统现在也将容易受到上述类型的场景的影响。

  2. 将其上传到主机,问题解决。


At the time of writing, there was a bug in chrome which required an xmlns attribute in order to trigger rendering:

<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" ... >

This was the problem I was running into when serving the xml file from a server.


If unlike me, you are viewing the xml file from a file:/// url, then the solutions mentioning --allow-file-access-from-files are the ones you want

相关问答

更多
  • 如果要访问参数,则需要使用$前缀。 也就是说,do contains($Type,'NEW')而不contains(Type,'NEW') 。 后者正在寻找一个名为Type的子元素,而不是传入的参数 试试这个模板。 请注意,您实际上不需要使用带有concat xsl:value-of来输出文本。
    Eric的另一个答案是错误的。 他提到的命名空间声明与这个问题无关。 它不起作用的真正原因是由于安全问题 (参见第4197 号 问题 , 第111905号 )。 想象这种情况: 您收到一封来自攻击者的电子邮件,其中包含您下载的附件的网页。 您可以在浏览器中打开现在的本地网页。 本地网页创建一个