node-XMLHttpRequest
node-XMLHttpRequest copied to clipboard
xhr.response is undefined on blob/arraybuffer response type
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?
Hi @nolash I met the same problem here, do you have any work around now?
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 ;)
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.
Just use http.get.
Any updates in that?
i'm just using the node library for file reads now