fetch icon indicating copy to clipboard operation
fetch copied to clipboard

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

Open sfuqua opened this issue 3 years ago • 2 comments

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.

sfuqua avatar Jul 21 '21 18:07 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.

https://github.com/github/fetch/issues/997#issue-950012817

MintThitiratChamnan avatar Aug 02 '21 17:08 MintThitiratChamnan