Skip to content

[p5.js 2.0+ Bug Report]: httpPost() stringifies FormData/Blob/URLSearchParams/TypedArray/null to JSON and strips headers from Request objects #9441

Backlog

Description

@DeepakMeena77

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:

  1. Call httpPost(url, data) with a standard Fetch BodyInit object (FormData, Blob, URLSearchParams, ArrayBuffer, or Uint8Array) or null.
  2. Or pass a Request instance with custom headers (such as Authorization or a custom Content-Type) as path to httpPost(req, data).
  3. Inspect the resulting Request body and headers created in fn.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):

  1. else if (typeof data === 'object') catches null, FormData, Blob, URLSearchParams, ArrayBuffer, and ArrayBufferView (TypedArray / DataView), replacing them with JSON.stringify(data) and 'Content-Type': 'application/json'. Checking data !== null and skipping JSON serialization for native Fetch BodyInit objects (preserving native Blob/URLSearchParams Content-Type and omitting Content-Type for FormData so Fetch sets the multipart boundary) fixes payload handling.
  2. When path instanceof Request, passing { headers: { 'Content-Type': contentType } } in requestOptions overwrites path.headers. Merging new Headers(path.headers) (and keeping any explicit Content-Type on path) 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.

Activity

  1. RjyavardhanSingh commented on Dec 2, 2026

    @RjyavardhanSingh

    Hi, I'd like to work on this. Could you assign it to me? I'll open a PR soon.

  2. DeepakMeena77 commented on Dec 2, 2026

    @DeepakMeena77
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions