Skip to content

body.blob() does not set Blob.type from Content-Type header #311

@godronus

Description

@godronus

When calling .blob() on a Request or Response, the resulting Blob always has an empty type, regardless of the Content-Type header on the request/response.

Per the Fetch spec - consume body, the Blob's type should be set to the body's extracted MIME type — derived from the Content-Type header, ASCII-lowercased.

WPT coverage

fetch/api/body/mime-type.any.js has 20 tests covering this behaviour.
The file currently cannot run: it contains a top-level bodyContainer.clone() call that throws TypeError because Response.clone() is not yet implemented, which aborts the file before any tests are registered. Once Response.clone() is implemented, 14 of those 20 tests will fail due to this bug.

Root cause

In builtins/web/fetch/request-response.cpp, parse_body<BodyReadResult::Blob> hardcodes contentType to the empty string:

} else if constexpr (result_type == RequestOrResponse::BodyReadResult::Blob) {
    JS::RootedString contentType(cx, JS_GetEmptyString(cx));  // always empty
    JS::RootedObject blob(cx, blob::Blob::create(cx, std::move(buf), len, contentType));

Fix

The FormData branch immediately below already does the correct thing — reads the Content-Type header via Headers::lookup and calls extract_mime_type. The Blob branch needs the same treatment, with the result ASCII-lowercased before being passed to Blob::create. The absent-header case (where blob.type should remain "") is already handled correctly by the current fallback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions