remix-run / remix

Build Better Websites. Create modern, resilient user experiences with web fundamentals.
https://remix.run
MIT License
29.79k stars 2.51k forks source link

Axios is not working properly (timeout) #3027

Closed binajmen closed 2 years ago

binajmen commented 2 years ago

What version of Remix are you using?

1.4.1

Steps to Reproduce

Use axios to query a third party service

Expected Behavior

Working query

Actual Behavior

Hi,

I've translated the axios request to the equivalent in fetch. It works flawlessly with fetch, it doesn't with axios (nothing happens till I kill the request – adding timeout: 1000 will throw a timeout error).

const response = await fetch("https://api.mollie.com/oauth2/tokens", {
  method: "post",
  headers: {
    "Content-Type": "application/json",
    Authorization:
      "Basic " +
      Buffer.from(
        `${process.env.MOLLIE_CLIENT_ID}:${process.env.MOLLIE_CLIENT_SECRET}`,
        "utf-8"
      ).toString("base64"),
  },
  body: JSON.stringify({
    grant_type: "authorization_code",
    code: code,
    redirect_uri: `https://localhost:3000/mollie-connect`,
  }),
});
console.log(await response.json());

Correctly returns:

{
  access_token: 'access_xxx',
  expires_in: 3600,
  token_type: 'bearer',
  scope: '...',
  refresh_token: 'refresh_xxx'
}

The axios equivalent doesn't:

const response: AxiosResponse<TokensResponse> = await axios({
  method: "post",
  url: "https://api.mollie.com/oauth2/tokens",
  data: {
    grant_type: "authorization_code",
    code: code,
    redirect_uri: `https://localhost:3000/mollie-connect`,
  },
  withCredentials: true,
  auth: {
    username: process.env.MOLLIE_CLIENT_ID,
    password: process.env.MOLLIE_CLIENT_SECRET,
  },
});

I migrated (copy/paste + fixed some import) my files from an old project ("remix": "^1.2.3") to the Blues stack ("@remix-run/node": "^1.4.1"). The latest query worked in the old environment. Could it be a change was introduced in the newest version?

I'm also using a third party library with axios as a dependency. Not working neither. Surely I did something wrong :sweat:

binajmen commented 2 years ago

In the Blues stack, there's a "conflict" between MSW & Axios.

MSN are aware of the issue in https://github.com/mswjs/msw/issues/1182#issuecomment-1090476127:

The latest MSW has an issue with how Axios works (see #1169).

You can temporarily downgrade MSW to 0.36.8.

magnusdr commented 2 years ago

Had the same problem. Removed --require ./mocks from dev:server-script in package.json as a temporary fix. Guess I won't be mocking third party resources until this is fixed.

raytiley commented 2 years ago

You can temporarily downgrade MSW to 0.36.8.

Just throwing this in here that downgrading to 0.36.8 to attempt to fix https://github.com/muxinc/mux-node-sdk seemingly broke aws-sdk/client-s3 . I've had to disable mocks in dev for now. Just putting this here in case anyone one else runs across it. Took me a few hours to realize there was even MSW in the stack that would be intercepting the sdk requests in dev.