Skip to content

Use of Content-Type (vs Accept) header for setting XHR responseType to 'arraybuffer' #997

@sfuqua

Description

@sfuqua

When fetching, we have this block to configure the outbound XHR:

    if ('responseType' in xhr) {
      if (support.blob) {
        xhr.responseType = 'blob'
      } else if (
        support.arrayBuffer &&
        request.headers.get('Content-Type') &&
        request.headers.get('Content-Type').indexOf('application/octet-stream') !== -1
      ) {
        xhr.responseType = 'arraybuffer'
      }
    }

I'm curious why the request's Content-Type is used as a heuristic for requesting an ArrayBuffer. For a GET request, we often won't have a Content-Type at all because there's no body in the request.

Should this instead be the Accept header, indicating the type of content that the sender is expecting to receive from the server?

My team is running into an issue where in order to get the desired behavior from the polyfill we need to add a Content-Type header to our requests which is a bit unnatural - it seems like Accept is a cleaner HTTP fit here.

This issue is exposed on React Native clients where ArrayBuffer works as expected (so we want an ArrayBuffer out of the XHR), but Blob does not, so support.blob is false.

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