Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to clear headers before retrying #11

Open
thejuan opened this issue Dec 7, 2020 · 2 comments
Open

Need to clear headers before retrying #11

thejuan opened this issue Dec 7, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@thejuan
Copy link

thejuan commented Dec 7, 2020

In case this helps anyone else using this in conjunction with axios-retry requires resetting the headers before re-signing.
I've added this after I add the aws4 interceptor.

 this.$axios.interceptors.request.use((config) => {
      // Need to remove old signing headers if this is a retry
      if ((config["axios-retry"] as any)?.retryCount > 0) {
        logger.info(`Cleaning headers from previous request`);
        for (const key of Object.keys(config.headers).filter((k) => k.startsWith("X-Amz"))) {
          delete config.headers[key];
        }
        delete config.headers["Authorization"];
      }
      return config;
    });
@philiptolk
Copy link

philiptolk commented Mar 30, 2021

I am having a similiar issue trying to change the account after setting it once.

const axios = require('axios');
const aws4Interceptor = require ("aws4-axios").aws4Interceptor;

function set_interceptor(sessionToken, accessKeyId, secretAccessKey){
  axios.interceptors.request.handlers.length = 0;
  const interceptor = aws4Interceptor({
        region: region,
        service: service
      }, {
        accessKeyId: accessKeyId,
        secretAccessKey: secretAccessKey,
        sessionToken: sessionToken
      });

    axios.interceptors.request.use(interceptor);
}

@eyalroth
Copy link

@thejuan Thank you for this discovery and workaround!

Here's my solution in TypeScript:

import axios, { AxiosRequestConfig } from 'axios';
import { aws4Interceptor } from 'aws4-axios';

type Config = AxiosRequestConfig & { _originalHeaders?: AxiosRequestConfig['headers'] };

const axios = axios.create();

axios.interceptors.request.use((config: Config) => {
  if (config._originalHeaders) {
    config.headers = config._originalHeaders;
  } else {
    config._originalHeaders = config.headers;
  }
  return config;
});

const interceptor = aws4Interceptor({...});
axios.interceptors.request.use(interceptor);

@jamesmbourne jamesmbourne added the bug Something isn't working label Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants