node-XMLHttpRequest icon indicating copy to clipboard operation
node-XMLHttpRequest copied to clipboard

xhr.response is undefined on blob/arraybuffer response type

Open nolash opened this issue 7 years ago • 6 comments

Trying to get this file with your nodejs xmlhttprequest

$ curl -X GET -I http://localhost/test/foo.bin
HTTP/1.1 200 OK
Date: Wed, 20 Jun 2018 11:58:51 GMT
Server: Apache/2.4.33 (Unix)
Last-Modified: Tue, 19 Jun 2018 18:37:07 GMT
ETag: "6-56f02f8230618"
Accept-Ranges: bytes
Content-Length: 6
Content-Type: application/octet-stream

using the following code:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

        var xhr = new XMLHttpRequest;
        xhr.open("GET", "http://localhost/test/foo.bin");
        xhr.setRequestHeader("Accept", "application/octet-stream");
        xhr.responseType = "blob";
        xhr.onreadystatechange = function(e) {
                if (xhr.readyState == 4) {
                        console.log(xhr.response);
                        console.log(xhr.responseText);
                }
        };      
        xhr.send();

Result is undefined for the logline with xhr.response and correct value in xhr.responseText.

Does your module only work for text?

nolash avatar Jun 20 '18 12:06 nolash

Hi @nolash I met the same problem here, do you have any work around now?

marlinilram avatar Jul 04 '18 14:07 marlinilram

Had the same issue with responseType "text". For me this worked as a workaround:

Object.defineProperty(
    XMLHttpRequest,
    'response',
    { get: () => this.responseText }
);

Note that you could change get function to check this.responseType and choose the appropriate property instead of just running this.responseText blindly ;)

mannebusk avatar Aug 30 '18 21:08 mannebusk

Is this fixed yet.. I want to get this to work.. I also get "undefined". The whole purpose of this lib is to run code already written and not change it. but how to get this to work? Do I need to edit the library by hand? the object in the debugger says undefined under responseText. Not sure what needs to be in the get function if it is not there.

bksubhuti avatar Mar 12 '20 13:03 bksubhuti

Just use http.get.

KR1470R avatar Jul 03 '20 17:07 KR1470R

Any updates in that?

vdenisenko-waverley avatar Sep 08 '20 09:09 vdenisenko-waverley

i'm just using the node library for file reads now

bksubhuti avatar Sep 08 '20 11:09 bksubhuti