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

postForm fails with "source.on is not a function" when using Buffer or Uint8Array in Node 18 #4727

Closed
smares opened this issue May 18, 2022 · 6 comments · Fixed by #4728
Closed

Comments

@smares
Copy link

smares commented May 18, 2022

Describe the issue

Passing Uint8Array or Buffer to postForm results in "source.on is not a function" exception.

Example Code

const data = Uint8Array.from([1, 2, 3])
axios.postForm('http://some.url', { data })

// or

const data = Uint8Array.from([1, 2, 3])
axios.postForm('http://some.url', { data: Buffer.from(data) })

Expected behavior, if applicable

axios should POST a field "data" with the content of the Uint8Array / Buffer

Environment

  • Axios Version 0.27.2
  • Node.js Version 18.2.0
  • OS: Windows 11

Additional context/Screenshots

Code is compiled from TypeScript using typescript 4.6.4 and a tsconfig.json that extends @tsconfig/node18/tsconfig.json

I am trying to upload the PDF output of puppeteer.

What am I doing wrong?

@smares smares changed the title postForm fails with "source.on is not a function" when using Buffer or Uint8Array postForm fails with "source.on is not a function" when using Buffer or Uint8Array in Node 18.2.0 May 18, 2022
@smares
Copy link
Author

smares commented May 18, 2022

So I was able to debug so far that what crashes is delayed-stream source.on('error', function() {}); which is used by combined-stream which is used by form-data. Question is still: Am I doing something wrong? Issue in form-data?

@DigitalBrainJS
Copy link
Collaborator

I don't seem to be able to reproduce the error at the moment

import axios from 'axios';

console.log(`Axios: v${axios.VERSION}`);

await axios.postForm('https://httpbin.org/post', {
  data: Uint8Array.from([1, 2, 3])
}).then(({data: {form, headers}})=> console.log({form, headers}), console.warn);


await axios.postForm('https://httpbin.org/post', {
  data: Buffer.from(Uint8Array.from([1, 2, 3]))
}).then(({data: {form, headers}})=> console.log({form, headers}), console.warn);
Axios: v0.27.2
{
  form: { data: '\x01\x02\x03' },
  headers: {
    Accept: 'application/json, text/plain, */*',
    'Content-Length': '202',
    'Content-Type': 'multipart/form-data; boundary=--------------------------299796871024925736357512',
    Host: 'httpbin.org',
    'User-Agent': 'axios/0.27.2',
    'X-Amzn-Trace-Id': 'Root=1-62851f35-4355336a4dbd8c9f21ea38b8'
  }
}
{
  form: { data: '\x01\x02\x03' },
  headers: {
    Accept: 'application/json, text/plain, */*',
    'Content-Length': '202',
    'Content-Type': 'multipart/form-data; boundary=--------------------------071752372898572766064691',
    Host: 'httpbin.org',
    'User-Agent': 'axios/0.27.2',
    'X-Amzn-Trace-Id': 'Root=1-62851f36-5e2ba27d392d30ff0f90e34d'
  }
}

Process finished with exit code 0

@smares
Copy link
Author

smares commented May 18, 2022

Which Node version? I just downgraded to 16.5.0 LTS and everything works. 18.1.0 and 18.2.0 produce my aforementioned error.

@DigitalBrainJS
Copy link
Collaborator

v16.10.0

@smares
Copy link
Author

smares commented May 18, 2022

Thanks for checking. So it affects only 18 and maybe 17.

@smares smares changed the title postForm fails with "source.on is not a function" when using Buffer or Uint8Array in Node 18.2.0 postForm fails with "source.on is not a function" when using Buffer or Uint8Array in Node 18 May 18, 2022
@DigitalBrainJS
Copy link
Collaborator

Yes, I just got the same error on v18.

DigitalBrainJS added a commit to DigitalBrainJS/axios that referenced this issue May 18, 2022
Added node 18.x to the CI;
Added hotfix for `ERR_OSSL_EVP_UNSUPPORTED` issue with karma running on node >=17.x;
Added `cross-env` to allow running build and test scripts on Windows platforms;
jasonsaayman added a commit that referenced this issue May 20, 2022
* Fixed bug #4727;
Added node 18.x to the CI;
Added hotfix for `ERR_OSSL_EVP_UNSUPPORTED` issue with karma running on node >=17.x;
Added `cross-env` to allow running build and test scripts on Windows platforms;

* Added conditional setting of `--openssl-legacy-provider` option for node versions >=17.x;

* Refactored ssl-hotfix & test script;

* Fixed and refactored default max body length test due to ECONNRESET failure;

* Added test for converting the data uri to a Blob;
Fixed bug with parsing mime type for Blob;

Co-authored-by: Jay <jasonsaayman@gmail.com>
jasonsaayman added a commit that referenced this issue May 28, 2022
* Fixing proxy beforeRedirect regression (#4708)

* Adding Canceler parameters config and request (#4711)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed `toFormData` regression bug (unreleased) with Array-like objects serialization; (#4714)

Added `toURLEncodedForm` helper;
Added automatic payload serialization to `application/x-www-form-urlencoded` to have parity with `multipart/form-data`;
Added test of handling `application/x-www-form-urlencoded` body by express.js;
Updated README.md;
Added missed param in JSDoc;
Fixed hrefs in README.md;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Allow webpack users to overwrite buildins (#4715)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed `AxiosError` status code type; (#4717)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed `AxiosError` stack capturing; (#4718)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* allow type definition for axios instance methods (#4224)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* add `string[]` to `AxiosRequestHeaders` type (#4322)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixing AxiosRequestHeaders typings (#4334)

Co-authored-by: Shakirov Kirill <ksshakirov@sberautotech.ru>
Co-authored-by: Jay <jasonsaayman@gmail.com>

* Added the ability for the `url-encoded-form` serializer to respect the `formSerializer` config; (#4721)

Added test for `formSerializer` config in context of `url-encoded-form` serializer;

* Updated eslint config; (#4722)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* fix: add isCancel type assert (#4293)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Added data URL support for node.js; (#4725)

* Added data URL support for node.js;
Added missed data URL protocol for the browser environment;
Optimized JSON parsing in the default response transformer;
Refactored project structure;
Added `cause` prop for AxiosError instance that refers to the original error if it was wrapped with `AxiosError.from` method;
Added fromDataURI helper;
Added test for handling data:url as an `arraybuffer|text|stream`;

* Added throwing of 405 HTTP error if the method is not GET;

* Fix/4263/maxbodylength defaults (#4731)

* test(http): add test case for default body length in follow-redirects

* fix(http): provide proper default body length to follow-redirects

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Adding types for progress event callbacks (#4675)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed bug #4727 : toFormData Blob issue on node>v17; (#4728)

* Fixed bug #4727;
Added node 18.x to the CI;
Added hotfix for `ERR_OSSL_EVP_UNSUPPORTED` issue with karma running on node >=17.x;
Added `cross-env` to allow running build and test scripts on Windows platforms;

* Added conditional setting of `--openssl-legacy-provider` option for node versions >=17.x;

* Refactored ssl-hotfix & test script;

* Fixed and refactored default max body length test due to ECONNRESET failure;

* Added test for converting the data uri to a Blob;
Fixed bug with parsing mime type for Blob;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* URL params serializer; (#4734)

* Refactored BuildURL helper to use URLSearchParams serializer;

* Updated typings;
Added TS test;

* Added `axios.formToJSON` method; (#4735)

* Draft

* Added `formDataToJSON` helper;
Added `axios.formToJSON` method;
Added client tests;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Bump grunt from 1.5.2 to 1.5.3 (#4743)

Bumps [grunt](https://github.com/gruntjs/grunt) from 1.5.2 to 1.5.3.
- [Release notes](https://github.com/gruntjs/grunt/releases)
- [Changelog](https://github.com/gruntjs/grunt/blob/main/CHANGELOG)
- [Commits](gruntjs/grunt@v1.5.2...v1.5.3)

---
updated-dependencies:
- dependency-name: grunt
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Updated README.md; (#4742)

Updated index.d.ts;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* chore: removed Travis CI config file as we have moved to GitHub actions

* chore: updated actions to run on new version based branches

* Fix/4737/timeout error message for http (#4738)

* Fixing timeoutErrorMessage in http calls

When timeoutErrorMessage was set this did not change anything in the error message, with this change the error message will be the configured message

* Testing timeoutErrorMessage in http calls

When timeoutErrorMessage was set this did not change anything in the error message, with this change the error message will be the configured message

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixing content-type header repeated (#4745)

Co-authored-by: Jay <jasonsaayman@gmail.com>

Co-authored-by: Maxime Bargiel <maxime.bargiel@gmail.com>
Co-authored-by: 毛呆 <aweikalee@163.com>
Co-authored-by: Dmitriy Mozgovoy <robotshara@gmail.com>
Co-authored-by: Tom Ceuppens <t-c@live.be>
Co-authored-by: Jelle Schutter <jelle@schutter.xyz>
Co-authored-by: Rraji Abdelbari <57002508+estarossa0@users.noreply.github.com>
Co-authored-by: Kirill Shakirov <kirill-shakirov@mail.ru>
Co-authored-by: Shakirov Kirill <ksshakirov@sberautotech.ru>
Co-authored-by: chenjigeng <178854407@qq.com>
Co-authored-by: Dimitris Halatsis <dimitris_ha@hotmail.com>
Co-authored-by: Johann Cooper <johanngcooper@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Victor Augusto <vdbernardes@gmail.com>
Co-authored-by: João Gabriel Quaresma <j.quaresmasantos_98@hotmail.com>
jasonsaayman added a commit that referenced this issue May 28, 2022
* Fixing proxy beforeRedirect regression (#4708)

* Adding Canceler parameters config and request (#4711)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed `toFormData` regression bug (unreleased) with Array-like objects serialization; (#4714)

Added `toURLEncodedForm` helper;
Added automatic payload serialization to `application/x-www-form-urlencoded` to have parity with `multipart/form-data`;
Added test of handling `application/x-www-form-urlencoded` body by express.js;
Updated README.md;
Added missed param in JSDoc;
Fixed hrefs in README.md;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Allow webpack users to overwrite buildins (#4715)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed `AxiosError` status code type; (#4717)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed `AxiosError` stack capturing; (#4718)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* allow type definition for axios instance methods (#4224)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* add `string[]` to `AxiosRequestHeaders` type (#4322)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixing AxiosRequestHeaders typings (#4334)

Co-authored-by: Shakirov Kirill <ksshakirov@sberautotech.ru>
Co-authored-by: Jay <jasonsaayman@gmail.com>

* Added the ability for the `url-encoded-form` serializer to respect the `formSerializer` config; (#4721)

Added test for `formSerializer` config in context of `url-encoded-form` serializer;

* Updated eslint config; (#4722)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* fix: add isCancel type assert (#4293)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Added data URL support for node.js; (#4725)

* Added data URL support for node.js;
Added missed data URL protocol for the browser environment;
Optimized JSON parsing in the default response transformer;
Refactored project structure;
Added `cause` prop for AxiosError instance that refers to the original error if it was wrapped with `AxiosError.from` method;
Added fromDataURI helper;
Added test for handling data:url as an `arraybuffer|text|stream`;

* Added throwing of 405 HTTP error if the method is not GET;

* Fix/4263/maxbodylength defaults (#4731)

* test(http): add test case for default body length in follow-redirects

* fix(http): provide proper default body length to follow-redirects

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Adding types for progress event callbacks (#4675)

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixed bug #4727 : toFormData Blob issue on node>v17; (#4728)

* Fixed bug #4727;
Added node 18.x to the CI;
Added hotfix for `ERR_OSSL_EVP_UNSUPPORTED` issue with karma running on node >=17.x;
Added `cross-env` to allow running build and test scripts on Windows platforms;

* Added conditional setting of `--openssl-legacy-provider` option for node versions >=17.x;

* Refactored ssl-hotfix & test script;

* Fixed and refactored default max body length test due to ECONNRESET failure;

* Added test for converting the data uri to a Blob;
Fixed bug with parsing mime type for Blob;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* URL params serializer; (#4734)

* Refactored BuildURL helper to use URLSearchParams serializer;

* Updated typings;
Added TS test;

* Added `axios.formToJSON` method; (#4735)

* Draft

* Added `formDataToJSON` helper;
Added `axios.formToJSON` method;
Added client tests;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Bump grunt from 1.5.2 to 1.5.3 (#4743)

Bumps [grunt](https://github.com/gruntjs/grunt) from 1.5.2 to 1.5.3.
- [Release notes](https://github.com/gruntjs/grunt/releases)
- [Changelog](https://github.com/gruntjs/grunt/blob/main/CHANGELOG)
- [Commits](gruntjs/grunt@v1.5.2...v1.5.3)

---
updated-dependencies:
- dependency-name: grunt
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Updated README.md; (#4742)

Updated index.d.ts;

Co-authored-by: Jay <jasonsaayman@gmail.com>

* chore: removed Travis CI config file as we have moved to GitHub actions

* chore: updated actions to run on new version based branches

* Fix/4737/timeout error message for http (#4738)

* Fixing timeoutErrorMessage in http calls

When timeoutErrorMessage was set this did not change anything in the error message, with this change the error message will be the configured message

* Testing timeoutErrorMessage in http calls

When timeoutErrorMessage was set this did not change anything in the error message, with this change the error message will be the configured message

Co-authored-by: Jay <jasonsaayman@gmail.com>

* Fixing content-type header repeated (#4745)

Co-authored-by: Jay <jasonsaayman@gmail.com>

Co-authored-by: Maxime Bargiel <maxime.bargiel@gmail.com>
Co-authored-by: 毛呆 <aweikalee@163.com>
Co-authored-by: Dmitriy Mozgovoy <robotshara@gmail.com>
Co-authored-by: Tom Ceuppens <t-c@live.be>
Co-authored-by: Jelle Schutter <jelle@schutter.xyz>
Co-authored-by: Rraji Abdelbari <57002508+estarossa0@users.noreply.github.com>
Co-authored-by: Kirill Shakirov <kirill-shakirov@mail.ru>
Co-authored-by: Shakirov Kirill <ksshakirov@sberautotech.ru>
Co-authored-by: chenjigeng <178854407@qq.com>
Co-authored-by: Dimitris Halatsis <dimitris_ha@hotmail.com>
Co-authored-by: Johann Cooper <johanngcooper@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Victor Augusto <vdbernardes@gmail.com>
Co-authored-by: João Gabriel Quaresma <j.quaresmasantos_98@hotmail.com>
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

Successfully merging a pull request may close this issue.

2 participants