0

I am requesting a URL of API service which responds with status code 307 (redirect) on success and a URL location in response Headers. This is protected API and needs Authorization header for it to be fulfilled successfully. The problem I am facing is that the Authorization header is not appended to the redirected URL, it is only appended to the original call, and therefore the redirected URL call fails. This works perfectly fine in Postman but is not working with Axios. I have tried it in ReactJS web app and React Native mobile app (I want it to work on React Native mobile app actually) with different versions of axios (i-e v0.19.2, and v0.27.2) and the result is the same. This can be duplicate of this but it is working fine with RN v0.62 and the problem has started only after upgrade to v0.67.4, and also, the URL to which it redirects is also from the same base URL so there shouldn't be any security concerns. I am wondering if there is a solution or patch to this needed to be applied after the upgrade.

A sample code snippet is below:

const axiosInstance = axios.create({ headers: { Authorization: token } });
const data = JSON.stringify({
      // ...data
    });

    var config = {
      method: "put",
      url:
        `https://${baseUrl}/coupons/apply?calculateTotal=true&customerId=10398&validateCoupon=true&validateProducts=true`,
      headers: {
        Authorization: "token",
        language: "EN",
        "Content-Type": "application/json",
      },
      data: data
    };

    axiosInstance(config)
      .then(function (response) {
        console.log(JSON.stringify(response.data));
      })
      .catch(function (error) {
        console.log("ERRRRRR", JSON.stringify(error));
      });

I expect that auth header should be appended to the redirected URL as well because it belongs to the same base URL. The redirected URL looks like https://{$baseUrl}/api/v1/cart?calculateTotal=true&customerId=10398&validateCoupon=true&validateProducts=true

The environment setup looks like:

Axios Version [0.19.2, ~0.27.2]

Adapter [HTTP]

Browser [JavascriptCore (JS engine in Safari)]

Node.js Version [14]

OS: [iOS 15.6.1, OSX 12.4]

Additional Library Versions [React 17.0.2, React 18.2.0, React Native 0.67.4]

Phil
  • 157,677
  • 23
  • 242
  • 245

0 Answers0