Repository navigation
Backlog
Labels
Description
Most appropriate sub-area of p5.js?
- Accessibility
- Color
- Core/Environment/Rendering
- Data
- DOM
- Events
- Image
- IO
- Math
- Typography
- Utilities
- WebGL
- WebGPU
- p5.strands
- Build process
- Unit testing
- Internationalization
- Friendly errors
- Other (specify if possible)
p5.js version
2.4.0 (main / src/io/files.js)
Web browser and version
Reproduced in Node.js v22.12.0 Fetch Request / FormData / Blob APIs (standard Web Fetch Request constructor used by browsers)
Operating system
Windows 11 (platform-independent JS Fetch Request handling)
Steps to reproduce this
Steps:
- Call
httpPost(url, data)with a standard FetchBodyInitobject (FormData,Blob,URLSearchParams,ArrayBuffer, orUint8Array) ornull. - Or pass a
Requestinstance with custom headers (such asAuthorizationor a customContent-Type) aspathtohttpPost(req, data). - Inspect the resulting
Requestbody and headers created infn.httpPost(src/io/files.js, lines 994–1021).
Snippet:
// 1. BodyInit objects & null are caught by `typeof data === 'object'`
const fd = new FormData();
fd.append('field', 'value');
// Expected: multipart/form-data body containing field=value
// Actual: reqData = JSON.stringify(fd) -> "{}", Content-Type = "application/json"
await httpPost('https://example.com/upload', fd);
// Same issue for Blob, URLSearchParams, Uint8Array/ArrayBuffer, and null:
// - new Blob(['hello'], { type: 'text/plain' }) -> body "{}", Content-Type: application/json
// - new URLSearchParams({ a: '1' }) -> body "{}", Content-Type: application/json
// - new Uint8Array([1, 2, 3]) -> body '{"0":1,"1":2,"2":3}', Content-Type: application/json
// - null -> body "null", Content-Type: application/json
// 2. Passing a Request object wipes its headers
const req = new Request('https://example.com/api', {
headers: { Authorization: 'Bearer secret', 'X-Custom': '1' }
});
// Line 1020 constructs `new Request(path, { method: 'POST', body: reqData, headers: { 'Content-Type': contentType } })`,
// replacing `req.headers` so Authorization and X-Custom become null.
await httpPost(req, 'hi');Cause & Proposed Fix
In src/io/files.js (fn.httpPost, lines 994–1021):
else if (typeof data === 'object')catchesnull,FormData,Blob,URLSearchParams,ArrayBuffer, andArrayBufferView(TypedArray/DataView), replacing them withJSON.stringify(data)and'Content-Type': 'application/json'. Checkingdata !== nulland skipping JSON serialization for native FetchBodyInitobjects (preserving nativeBlob/URLSearchParamsContent-Typeand omittingContent-TypeforFormDataso Fetch sets the multipart boundary) fixes payload handling.- When
path instanceof Request, passing{ headers: { 'Content-Type': contentType } }inrequestOptionsoverwritespath.headers. Mergingnew Headers(path.headers)(and keeping any explicitContent-Typeonpath) preserves caller headers as documented ("Both method and body will be overridden").
I verified this on main (src/io/files.js) and would be happy to open a PR with unit tests in test/unit/io/files.js once maintainers confirm the expected behavior.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
RjyavardhanSingh commentedon Dec 2, 2026
Hi, I'd like to work on this. Could you assign it to me? I'll open a PR soon.
DeepakMeena77 commentedon Dec 2, 2026
Hi @RjyavardhanSingh, thanks for the interest, though as noted at the bottom of the report I already traced the fix and plan to open the PR with unit tests once maintainers confirm.