node.js 如何通过 superagent 使用 cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11919737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:08:37  来源:igfitidea点击:

How can you use cookies with superagent?

node.jsmochasuperagent

提问by Kuryaki

I'm doing cookie session management with express with something like this:

我正在使用 express 进行 cookie 会话管理,如下所示:

req.session.authentication = auth;

And I verify the authenticated urls with something like

我用类似的东西验证经过身份验证的网址

if(!req.session.authentication){res.send(401);}

Now I'm building tests for the URLs with mocha, superagentand should, however I can't seem to find a way to get/set the cookie with superagent. I even tried to request the login before the authenticated test but it is not working,

现在我正在使用 mocha、superagentshould为 URL 构建测试,但是我似乎无法找到使用 superagent 获取/设置 cookie 的方法。我什至试图在经过身份验证的测试之前请求登录,但它不起作用,

I have tried adding the request to the login in the before statement for the mocha BDD suite, however it is still telling me that the request is unauthorized, I have tested the authentication doing the requests from the browser, however it is not working from the suite any ideas why?

我已经尝试在 mocha BDD 套件的 before 语句中将请求添加到登录中,但是它仍然告诉我该请求未经授权,我已经测试了从浏览器发出请求的身份验证,但是它无法从套件任何想法为什么?

采纳答案by Gaurav

Use superagent.agent()(instead of plain old superagent) to make requests have persistent cookies. See 'Preserving cookies' in the superagent docs, or the code examples: agency.js, controller.test.js.

使用superagent.agent()(而不是普通的 old superagent)使请求具有持久性 cookie。请参阅superagent 文档中的“保留 cookie”,或代码示例:agency.jscontroller.test.js

回答by Teoman shipahi

Seems like following code works fine;

似乎以下代码工作正常;

req.set('Cookie', "cookieName1=cookieValue1;cookieName2=cookieValue2");

req.set('Cookie', "cookieName1=cookieValue1;cookieName2=cookieValue2");

回答by Denys Feshchenko

If the issue is in sending cookies for CORS requests use .withCredentials()method described here

如果问题在于为 CORS 请求发送 cookie,请使用此处描述的.withCredentials()方法

request
  .get('http://localhost:4001/')
  .withCredentials()
  .end(function(err, res) { })

回答by BAR

Since you mentioned you need to both get andset the cookie:

由于您提到您需要同时获取设置 cookie:

Get:

得到:

const request = await Superagent.get('...')

const cookie = request.header['set-cookie']

Set:

放:

Superagent.post('...').set('Cookie', 'cookie_info')

回答by Sebastien Horin

2020 +

2020 +

A clean way to do it is:

一个干净的方法是:

  • create a simple cookie store
  • abstract set Cookie to send it in each request
  • update the cookie only when needed
  • 创建一个简单的 cookie 存储
  • 抽象设置 Cookie 在每个请求中发送它
  • 仅在需要时更新 cookie

Note I keep the same URL because I use graphql but you can make it a parameter:

注意我保留相同的 URL,因为我使用了 graphql,但您可以将其设置为参数:

const graph = agent =>
  agent.post('/graph')
    .set('cookie', cookieStore.get());

const handleCookie = res =>
  cookieStore.set(res.headers['set-cookie'][0]);

let currentCookie="";
const cookieStore = {
  set: cookie=>{currentCookie=cookie},
  get: cookie=>currentCookie,
};

module.exports = {graph,connectTestUser,handleCookieResponse};

You can now just use graph(agent)to send a request and handleCookie(response)when you have a response that may update your cookie (set or clear), example:

当您有可能更新您的 cookie(设置或清除)的响应时,您现在可以使用graph(agent)发送请求和handleCookie(response),例如:

graph(agent).end((err,res) => {
  if (err) return done(err);
  res.statusCode.should.equal(200);
  handleCookie(res);
  return done();
});