Skip to content

Automatically detect the content type retrieved by fetch() #678

Open
@garysassano

Description

@garysassano

When interacting with a server that can respond with either JSON or an HTML document, is there a way to gracefully handle the response with minimal code? Specifically, I want to attempt parsing the response as JSON and, if that fails, fall back to treating it as plain text. Additionally, I would like the response.clone() to be handled automatically in the background.

Expected Behavior

  • Attempt to parse the response as JSON.
  • Fallback to plain text if JSON parsing fails.
  • Automatically handle response.clone() to avoid issues with consuming the body multiple times.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions