Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sfuqua opened this issue Jul 21, 2021 · 2 comments

Comments

@sfuqua
Copy link

@sfuqua sfuqua commented Jul 21, 2021

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.

@github github deleted a comment Jul 22, 2021
@MintThitiratChamnan
Copy link

@MintThitiratChamnan MintThitiratChamnan commented Aug 2, 2021

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.

#997 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@sfuqua @MintThitiratChamnan and others