PostMan上的Nodejs上的Axios不会在请求的服务器上保留会话 [英] Axios on Nodejs wont retain session on requested server while PostMan does

查看:283
本文介绍了PostMan上的Nodejs上的Axios不会在请求的服务器上保留会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在PostMan上执行以下操作



1)POST方法登录公司服务器。
2)以公司服务器上的登录用户身份发出其他请求。



我创建了一个nodejs应用程序来与公司服务器通信。
我正在使用axios库进行上述通信。



登录到公司服务器后,任何其他呼叫都不会将我识别为授权用户。 / p>

我可以依次在axios上重新创建以实现会话持久性的区别是什么?

解决方案

在客户端,您仅在axios中使用withCredentials参数-此选项自动保存请求之间的会话。但是在node.js环境中,此参数不起作用,因为axios使用http node.js模块而不是XHR。



在node env中,您可以使用axios实例在请求之间保存cookie 。



最简单的方法是



创建实例

  const BASE_URL = https://stackoverflow.com; 

//可与BASE_URL一起使用的axios的初始化实例
const axiosInstance = axios.create({baseURL:BASE_URL});

编写createSession函数

  const createSession = async()=> {
console.log(创建会话);
const authParams = {
用户名:用户名,
密码:密码
};
const resp =等待axios.post(BASE_URL,authParams);
const cookie = resp.headers [ set-cookie] [0]; //从请求
中获取cookie axiosInstance.defaults.headers.Cookie = cookie; //将cookie附加到axiosInstance以用于将来的请求
};

并使用Cookie拨打电话

  //在创建会话
createSession()之后将发请求发送到https://stackoverflow.com/protected。then(()=> {
axiosInstance.post('/ protected')//具有新cookie
}}

请注意,您的授权方法可能与所提供的方法有所不同-在这种情况下,您只需更改 createSession 方法即可。如果会话期满,则可以直接登录,也可以使用axios.interceptors-我附加了gist的链接。



此外,您还可以在与cookie-jar的连接中使用axios。 (下面的链接)



有关更多信息:




I am able to do the following on PostMan

1) POST method to login to company server. 2) Make other requests as a logged in user on company server.

I have created a nodejs app to communicate with the company server. I am using axios library to make said communications.

after Logging in to company server, any other calls don't recognize me as an authorized user.

What could be the differences that i could in turn recreate on axios to have that session persistance?

解决方案

On client side you just use withCredentials params in axios - this option automatically save your session between requests. But in node.js enviroment this parameter not work cause axios uses http node.js module instead of XHR.

In node env you can use axios instance for save cookie between requests.

Simplest way is

Create instance

const BASE_URL = "https://stackoverflow.com";

// Init instance of axios which works with BASE_URL
const axiosInstance = axios.create({ baseURL: BASE_URL });

Write createSession function

const createSession = async () => {
  console.log("create session");
  const authParams = {
    username: "username",
    password: "password"
  };
  const resp = await axios.post(BASE_URL, authParams);
  const cookie = resp.headers["set-cookie"][0]; // get cookie from request
  axiosInstance.defaults.headers.Cookie = cookie;   // attach cookie to axiosInstance for future requests
};

And make call with cookie

// send Post request to https://stackoverflow.com/protected after create session 
createSession().then(() => {
  axiosInstance.post('/protected') // with new cookie
})

Be careful, your authorization method may differ from the presented - on this case you can just change the createSession method. If your session is expire, you can login again directly or using axios.interceptors - i attached a link to gist.

Also you can use axios in connect with cookie-jar (link below)

For more info:

这篇关于PostMan上的Nodejs上的Axios不会在请求的服务器上保留会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆