2

I am using a library to make an HTTP call, I know it is using XMLHTTPRequest, but I can't set responseType and response is always text.

I need to convert the text to ArrayBuffer as I used responseType: ArrayBuffer. I tried multiple conversion but none of them produce the same buffer.

Update: So the code below is what converts the ArrayBuffer to the string.

    var dataView = new DataView(this.response);
    var decoder = new TextDecoder('utf8');
    var decodedString = decoder.decode(dataView);

I need to reverse this

   var encodedr = new TextEncoder('utf8');              
   var encodedArray =encodedr.encode(req.body);
   var arrayBuffer = encodedArray.buffer;

is not the same and is almost twice the size.

Update 2: Glitch with code example https://successful-pepper.glitch.me/

6
  • Can you specify the library name? It could be helpful Commented Dec 7, 2019 at 1:05
  • it is developer.atlassian.com/cloud/jira/platform/jsapi/request Commented Dec 7, 2019 at 1:31
  • 1
    It seems that their documentation says you can have as responseType JSON by default or text, no other options. If you can't change library, I suggest converting the text you receive in ArrayBuffer (see an example string to ArrayBuffer here developers.google.com/web/updates/2012/06/…). If you already tried that, please provide what you tried until now Commented Dec 7, 2019 at 1:42
  • @Lykos94 I tried that and that is not producing the same buffer. Commented Dec 7, 2019 at 6:44
  • @koolaang looks like your req.body in second part is ArrayBuffer already? try to use it without encoding, because your code works as expected stackblitz.com/edit/js-rfyuga , but if you are encoding something that has type ArrayBuffer already it will be double sized Commented Dec 9, 2019 at 11:31

1 Answer 1

1

The only issue I see in the glitch you've posted is that you're trying to compare 2 ArrayBuffers by reference, and not by value.

In JavaScript, non-primitive values are given a reference in the memory. So, when comparing non-primitive values, their reference in the memory is being compared instead of their value.

A simple exaple:

var firstArray = [1, 2, 3];
var secondArray = [1, 2, 3];

console.log(firstArray === secondArray); // prints: false

You can try to use JSON.stringify() to convert both ArrayBuffers to JSON and compare these values. You will, then, get that both ArrayBuffers are the same.

Another way to test if two ArrayBuffers are the same can be found here: How to test for equality in ArrayBuffer, DataView, and TypedArray

Sign up to request clarification or add additional context in comments.

2 Comments

yes you are right about comparison, I meant to compare the size, I will update it. but how can an ArrayBuffer with size of 6178243 be the same as an ArrayBuffer with the size of 3406624?
Well, it is not. When I open the console on the glitch, I get converted Uint8Array Uint8Array(6178243) and we converted string to arrrayBuffer ArrayBuffer(6178243) which is the same size.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.