0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

axiosのインターセプター axios.interceptors.response.use()

Posted at

axios.interceptors.response.use()

axios.interceptors.response.use() は、APIからのレスポンスが返された直後、またはエラーが発生した場合に実行される関数を指定することができる。

このメソッドは以下の2つのコールバック関数を引数として提供する。

Success Callback

最初のコールバックは、レスポンスが成功した場合に実行される。このコールバックは、レスポンスを受け取り、必要に応じてレスポンスを変更または加工し、その後の処理に渡すことができる。

Error Callback

2つ目のコールバックは、レスポンスがエラーを返した場合、またはリクエスト中に何らかのエラーが発生した場合に実行される。このコールバックを使用して、エラーハンドリングやエラーロギング、特定のエラーレスポンスに基づくアクションの実行などの処理を行うことができる。

// apiClientはaxiosのインスタンス
apiClient.interceptors.response.use(
  function(response) {
    // 何らかの成功時の処理。例えば、レスポンスデータを変更することもできます。
    return response;
  },
  function(error) {
    // 何らかのエラーハンドリング。例えば、特定のエラーコードに応じてリダイレクトすることもできます。
    return Promise.reject(error);
  }
);

インターセプタの設定により、全てのAPIリクエストのレスポンスに対して一貫した処理を適用することができる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?