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

Getting 400 Bad Request in v1.2.0 with same code that returned 200 with v0.27.2 #5311

Open
jamesmortensen opened this issue Nov 25, 2022 · 16 comments

Comments

@jamesmortensen
Copy link

Describe the bug

We are making a get request to an endpoint that works great, returning 200 OK with v0.27.2 of axios, but when we upgrade to 1.0.0 or to the latest 1.2.0, we get 400 Bad requests.

To Reproduce

// run with node index.js

(async () => {
  const instance = axios.create({
    baseURL: 'https://DOMAIN_SANITIZED/api/internal/v1',
    timeout: 1000,
    headers: { 'Authorization': 'Bearer SANITIZED' }
  });


  const response = await instance.get('/request/path/sanitized?q={"merchantId":"SANITIZED 32 CHAR GUID","scheduleType":"CLASS","isDeleted":false}');


  console.log(response.data);
  console.log(response.status);
  //console.log(response.statusText);
  //console.log(response.headers);
  //console.log(response.config);

})();

Code snippet

No response

Expected behavior

We expect the latest version to work the same as 0.27.2.

Axios Version

1.2.0

Adapter Version

UNKNOWN

Browser

N/A

Browser Version

N/A

Node.js Version

16.17.0

OS

macOS 13.0.1 M1

Additional Library Versions

No additional libs. The code snippet included is a minimal reproduceable example, which has been sanitized.

Additional context/Screenshots

We are unable to include the exact payload that caused the problem since it requires authentication.  We suspect/guess that maybe it is some kind of URL encoding issue that may have been introduced in v1.0.0.
@karlhorky
Copy link
Contributor

karlhorky commented Nov 25, 2022

Also seeing new 400 error status codes on axios@1.2.0, where they suceeded on axios@1.1.3, maybe some regression?

I'm fetching to the Notion unofficial API using a cookie:

const client = axios.create({
  // Notion unofficial API
  baseURL: 'https://www.notion.so/api/v3',
  headers: {
    Cookie: `token_v2=${process.env.NOTION_TOKEN}`,
  },
});

https://github.com/upleveled/notion-backup/blob/36d1ed97f34dd426fecf1ea800f45c432235b640/index.js#L51-L57

@jsiegenthaler
Copy link

jsiegenthaler commented Nov 27, 2022

I'm seeing axios@1.2.0 fail with 401 error where exact same code on axios@1.1.3 produces 200 OK

@karlhorky
Copy link
Contributor

@jasonsaayman any idea on this one? Any way a breaking change / regression could have slipped in to axios@1.2.0? I haven't had a chance to look in detail yet...

@jsiegenthaler
Copy link

jsiegenthaler commented Nov 27, 2022

@karlhorky See also #5314, I'm having issues as well, and I've seen a difference in responses between axios@1.2.0 and axios@1.1.3

@karlhorky
Copy link
Contributor

karlhorky commented Nov 27, 2022

Looking at the 1.2.0 changelog, wonder if this could be caused by the change to the new AxiosHeaders class by @DigitalBrainJS:

Considering that both of the 2 reproductions above have headers...

Digging in a bit to our code to check what the differences are with the request...

Edit: It seems #5224 was unrelated, see below.

@karlhorky
Copy link
Contributor

karlhorky commented Nov 27, 2022

Ok, it seems that the response problem fixed in #5306 (and axios@1.2.0-alpha.1) was affecting my requests - I was using a property from the previous response without checking the type (it was undefined). I've built in a check now 😅

const client = axios.create({
  baseURL: 'https://www.notion.so/api/v3',
  headers: { Cookie: `token_v2=${process.env.NOTION_TOKEN}` },
});

const { data: { taskId } }: { data: { taskId: string } } = await client.post('enqueueTask', {
  task: {
    // ...
  },
});

+if (!taskId) {
+  throw new Error('No taskId returned from enqueueTask');
+}

console.log(`Started export of block ${block.dirName} as task ${taskId}`);

const { data: { results } }: { data: { results: Task[] } } = await client.post('getTasks', {
  taskIds: [taskId], // 💥 taskId was undefined before, leading to "400 Bad request" errors
});

Not sure if this also affects you in the same way @jamesmortensen ...

@jasonsaayman
Copy link
Member

Can you try with the latest version and let me know if this is still an issue

@karlhorky
Copy link
Contributor

karlhorky commented Dec 9, 2022

Ok, I tested axios@1.2.1 and it does not succeed, but it also does not fail in the same way!

The taskId I mentioned above is now coming back defined 🙌

But now I have this one:

file:///home/runner/work/notion-backup/notion-backup/node_modules/axios/lib/core/AxiosError.js:89
  AxiosError.call(axiosError, error.message, code, config, request, response);
             ^
AxiosError: unexpected end of file
    at AxiosError.from (file:///home/runner/work/notion-backup/notion-backup/node_modules/axios/lib/core/AxiosError.js:89:14)
    at BrotliDecompress.handleStreamError (file:///home/runner/work/notion-backup/notion-backup/node_modules/axios/lib/adapters/http.js:489:29)
    at BrotliDecompress.emit (node:events:5[25](https://github.com/upleveled/notion-backup/actions/runs/3657855444/jobs/6181916764#step:7:26):35)
    at BrotliDecompress.emit (node:domain:489:12)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'Z_BUF_ERROR',
  errno: -5,

...

method: 'POST',
path: '/api/v3/getTasks',

...

// Request headers
[Symbol(kOutHeaders)]: [Object: null prototype] {
  accept: [ 'Accept', 'application/json, text/plain, */*' ],
  'content-type': [ 'Content-Type', 'application/json' ],
  cookie: [
    'Cookie',
    'token_v2=***'
  ],
  'user-agent': [ 'User-Agent', 'axios/1.2.1' ],
  'content-length': [ 'Content-Length', '442' ],
  'accept-encoding': [ 'Accept-Encoding', 'gzip, compress, deflate, br' ],
  host: [ 'Host', 'www.notion.so' ]
},

...

// Response headers
2022-12-09T13:41:07.2960756Z     res: IncomingMessage {
...
2022-12-09T13:41:07.3026915Z           method: 'POST',
2022-12-09T13:41:07.3027259Z           headers: [Object: null prototype] {
2022-12-09T13:41:07.3027664Z             Accept: 'application/json, text/plain, */*',
2022-12-09T13:41:07.3028039Z             'Content-Type': 'application/json',
2022-12-09T13:41:07.3029035Z             Cookie: 'token_v2=***',
2022-12-09T13:41:07.3029367Z             'User-Agent': 'axios/1.2.1',
2022-12-09T13:41:07.3029695Z             'Content-Length': '442',
2022-12-09T13:41:07.3030078Z             'Accept-Encoding': 'gzip, compress, deflate, br'
2022-12-09T13:41:07.3030335Z           },

...

  cause: Error: unexpected end of file
      at __node_internal_genericNodeError (node:internal/errors:863:15)
      at BrotliDecoder.zlibOnError [as onerror] (node:zlib:189:17) {
    errno: -5,
    code: 'Z_BUF_ERROR'
  }

My code is like this - I'm fetching to the Notion unofficial API using a cookie:

const client = axios.create({
  // Notion unofficial API
  baseURL: 'https://www.notion.so/api/v3',
  headers: {
    Cookie: `token_v2=${process.env.NOTION_TOKEN}`,
  },
});

...

await client.post('enqueueTask', {
  task: {
    eventName: 'exportBlock',
    request: {
      blockId: block.id,
      exportOptions: {
        exportType: 'markdown',
        locale: 'en',
        timeZone: 'Europe/Vienna',
      },
      recursive: block.recursive,
    },
  },
});

https://github.com/upleveled/notion-backup/blob/cb9c26537aee56e5f540a748bd08763de42a45f0/index.ts#L48-L54

cc @DigitalBrainJS

@karlhorky
Copy link
Contributor

Maybe I need to disable Brotli decompression or something?

@karlhorky
Copy link
Contributor

karlhorky commented Dec 9, 2022

Maybe the problem that I'm getting now has to deal with downloading the data from the stream:

const response = await client<Stream>({
  method: 'GET',
  url: block.task.status.exportURL || undefined,
  responseType: 'stream',
});

const sizeInMb = Number(response.headers['content-length']) / 1000 / 1000;
console.log(`Downloading ${Math.round(sizeInMb * 1000) / 1000}mb...`);

const stream = response.data.pipe(createWriteStream(temporaryZipPath));

await new Promise((resolve, reject) => {
  stream.on('close', resolve);
  stream.on('error', reject);
});

https://github.com/upleveled/notion-backup/blob/cb9c26537aee56e5f540a748bd08763de42a45f0/index.ts#L147-L151

karlhorky added a commit to upleveled/notion-backup that referenced this issue Dec 9, 2022
karlhorky added a commit to upleveled/notion-backup that referenced this issue Dec 9, 2022
@ADumaine
Copy link

ADumaine commented Dec 9, 2022

I just updated to 1.2.1 from 1.1.3 and am getting the same end of file error:

(node:17800) UnhandledPromiseRejectionWarning: Error: unexpected end of file
    at AxiosError.from (C:\Data\ZENDev\multisigapp\node_modules\axios\dist\node\axios.cjs:785:14)
    at BrotliDecompress.handleStreamError (C:\Data\ZENDev\multisigapp\node_modules\axios\dist\node\axios.cjs:2696:29)
    at BrotliDecompress.emit (node:events:525:35)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:83:21)

@jsiegenthaler
Copy link

jsiegenthaler commented Dec 9, 2022 via email

@barchandune
Copy link

I am also seeing same error with 1.2.1

AxiosError: Request failed with status code 400
    at settle (file:///Users/a0k01ps/Github_home/smartlist-itemservice-checker/node_modules/axios/lib/core/settle.js:19:12)
    at IncomingMessage.handleStreamEnd (file:///Users/a0k01ps/Github_home/smartlist-itemservice-checker/node_modules/axios/lib/adapters/http.js:505:11)
    at IncomingMessage.emit (node:events:402:35)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ERR_BAD_REQUEST',

@jsiegenthaler
Copy link

jsiegenthaler commented Dec 13, 2022

I am getting the same error in v1.2.1 as well:

/usr/lib/node_modules/myProjectName/node_modules/axios/lib/core/settle.js:19
    reject(new AxiosError(
           ^
AxiosError: Request failed with status code 401
    at settle (/usr/lib/node_modules/homebridge-eosstb/node_modules/axios/lib/core/settle.js:19:12)
    at IncomingMessage.handleStreamEnd (/usr/lib/node_modules/homebridge-eosstb/node_modules/axios/lib/adapters/http.js:497:11)
    at IncomingMessage.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at processTicksAndRejections (node:internal/process/task_queues:82:21)

@karlhorky
Copy link
Contributor

karlhorky commented Dec 16, 2022

Wondering - maybe these AxiosError: unexpected end of file errors that we're experiencing in axios@1.2.1 are fixed by #5353 (PR merged but not released)

cc @DigitalBrainJS

@karlhorky
Copy link
Contributor

karlhorky commented Dec 30, 2022

Seems like these AxiosError: unexpected end of file problems are fixed in axios@1.2.2

@jamesmortensen can you check, and if confirmed, close the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants