aurelia / http-client

A simple, restful, message-based wrapper around XMLHttpRequest.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Saving Response Blob Data Issue

S4RD7R opened this issue · comments

I'm submitting a bug report

  • Library Version:
    http-client 1.0.0

Please tell us about your environment:

  • Operating System:
    OSX 10.11.6
  • Node Version:
    6.2.2
  • NPM Version:
    3.10.3
  • JSPM OR Webpack AND Version
    JSPM 0.16.39
  • Browser:
    Electron v1.2.5
  • Language:
    ESNext

Current behavior:
I am using the aurelia-http-client library to get binary data from a REST service, and store it to file
(using a Blob).

According to what I is in #93, the data should be in message.content

This is the code I have been trying.

`
.withResponseType('blob')

        .withInterceptor({
            response(message) {
                var defaultFileName = "default.txt";
                var disposition = message.headers.headers['content-disposition']?message.headers.headers['content-disposition']:message.headers.headers['Content-Disposition'];
                if (disposition) {
                    var match = disposition.match(/.*filename=\"?([^;\"]+)\"?.*/);
                    if (match[1])
                        defaultFileName = match[1];
                }
                defaultFileName = defaultFileName.replace(/[<>:"\/\\|?*]+/g, '_');
                fs.writeFileSync(defaultFileName, message.content);

...
`

Expected/desired behavior:
Either there is an issue in message.content or I am way off track.

@bryanrsmith Can you respond to this?

@MMJM Can you provide more information? Do you see an error, or some other unexpected behavior?

If I had interpreted right the OP issue, I'm experiencing a similar problem: I need to get the response content as Blob, but the client gives me a string (failing for binary data). How can I tell the client to return the content as Blob?

In fetch we have fetch(url).then(res => res.blob()) but I can't switch to fetch right now…

Okay, turning client.get(uri) to client.createRequest(uri).withResponseType("blob").send() made it work! Now response.content is a shiny Blob 👍