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

undefined in response.body when responseType is 'blob' or 'arraybuffer' #12

Closed
Aybee5 opened this issue Apr 17, 2024 · 8 comments · Fixed by #16
Closed

undefined in response.body when responseType is 'blob' or 'arraybuffer' #12

Aybee5 opened this issue Apr 17, 2024 · 8 comments · Fixed by #16
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@Aybee5
Copy link

Aybee5 commented Apr 17, 2024

When I set the responseType: 'blob' for a request, the response.body is undefined, similar to the following axios issue
axios/axios#1392

Example something like this

xior.post(
  'http://assets.xxxxx.org:3000/uploads/3ba6921c-031e-426b-94a3-a4b966fc145f/documents/0f6a1d1166.pdf',
{
  headers: {
    Accept: 'application/pdf',
  },
  responseType: 'blob',
},
)
.then(response => {
  console.log(response.data); //undefined
})
@suhaotian
Copy link
Owner

suhaotian commented Apr 17, 2024

The post method paramater should be: xior.post('apiUrl', {}, {responseType: 'blob'})

When responseType: 'blob', xior don't do anything to the response, it will return the original response like fetch:

xior.get('https://exmaple.com/some/api', null, {responseType: 'blob'}).then(res => {
  console.log(res.response)
});

Same with fetch's response:

fetch('https://exmaple.com/some/api').then(response => {
  console.log(response)
});

BTW, what you want do with this blob?

if you want download pdf, you can:

// For browser
xior
  .post(
    'http://assets.xxxxx.org:3000/uploads/3ba6921c-031e-426b-94a3-a4b966fc145f/documents/0f6a1d1166.pdf',
    null,
    {
      headers: {
        Accept: 'application/pdf',
      },
      responseType: 'blob',
    }
  )
  .then((res) => res.response.blob())
  .then((blob) => {
    var url = window.URL.createObjectURL(blob);
    var a = document.createElement('a');
    a.href = url;
    a.download = 'filename.pdf';
    document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
    a.click();
    a.remove(); //afterwards we remove the element again
  });

For nodejs:

https://github.com/suhaotian/xior?tab=readme-ov-file#3-how-do-i-handle-responses-with-types-like-stream-document-arraybuffer-or-blob

@Aybee5
Copy link
Author

Aybee5 commented Apr 17, 2024

Thank you for your response.

Yes, I am passing the options as the third argument, let me see if I can get a public URL I can use as a production.


This is what I am currently doing, I just wanted to bring your attention to it in case it's a bug 🐛

if you want download pdf, you can:

// For browser
xior
  .post(
    'http://assets.xxxxx.org:3000/uploads/3ba6921c-031e-426b-94a3-a4b966fc145f/documents/0f6a1d1166.pdf',
    null,
    {
      headers: {
        Accept: 'application/pdf',
      },
      responseType: 'blob',
    }
  )
  .then((res) => res.response.blob())
  .then((blob) => {
    var url = window.URL.createObjectURL(blob);
    var a = document.createElement('a');
    a.href = url;
    a.download = 'filename.pdf';
    document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox
    a.click();
    a.remove(); //afterwards we remove the element again
  });

@suhaotian
Copy link
Owner

Currently, xior just return the original fetch response when responseType is not 'undefined', 'text' or 'json'.

And I update the README. Thank you for report this 🙌

@suhaotian suhaotian added documentation Improvements or additions to documentation good first issue Good for newcomers labels Apr 17, 2024
@suhaotian
Copy link
Owner

Yeah, it will be a little confuse, I will reopen this issue, and fix in next version.

Again, thanks for the feedback!

@suhaotian suhaotian reopened this Apr 17, 2024
@Aybee5
Copy link
Author

Aybee5 commented Apr 18, 2024

Thank you, suhaotian

@Aybee5
Copy link
Author

Aybee5 commented Apr 18, 2024

Also, I'll like to make this contribution.
From my understanding, the changes will be somewhere around https://github.com/suhaotian/xior/blob/main/src%2Fxior.ts#L290, right?

@suhaotian
Copy link
Owner

@Aybee5 Welcome contribution!

It's Here: https://github.com/suhaotian/xior/blob/main/src%2Fxior.ts#L319

@suhaotian
Copy link
Owner

Merged then the issue automatic closed! (well done, github!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
2 participants