LoginSignup
4
2

More than 5 years have passed since last update.

axiosでrequestと同じようにjarを使えるようにする方法

Posted at

メモ

ほぼ自分用のメモです axiosでrequestの時に使っていたjarが簡単に使えませんでしたが、他のライブラリを導入することで解決できるようです。

request = request.defaults({jar: true}); 

requestだとこんな感じのコードのやつです。

必要なモジュールの導入

yarnで入れているのでnpmの方は yarn addnpm i とかに置き換えてください。

yarn add axios --dev
yarn add axios-cookiejar-support --dev
yarn add tough-cookie --dev

コード

const axiosCookieJarSupport = require('axios-cookiejar-support').default;
const tough = require('tough-cookie');
const cookieJar = new tough.CookieJar();
axiosCookieJarSupport(axios);
axios.defaults.withCredentials = true;
axios.defaults.jar = cookieJar;

参考資料

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2