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/

koolaang
  • 397
  • 6
  • 15
  • Can you specify the library name? It could be helpful – Lykos94 Dec 07 '19 at 01:05
  • it is https://developer.atlassian.com/cloud/jira/platform/jsapi/request/ – koolaang Dec 07 '19 at 01: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 https://developers.google.com/web/updates/2012/06/How-to-convert-ArrayBuffer-to-and-from-String). If you already tried that, please provide what you tried until now – Lykos94 Dec 07 '19 at 01:42
  • @Lykos94 I tried that and that is not producing the same buffer. – koolaang Dec 07 '19 at 06: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 https://stackblitz.com/edit/js-rfyuga , but if you are encoding something that has type `ArrayBuffer` already it will be double sized – nickbullock Dec 09 '19 at 11:31
  • @nickbullock sorry, I don't get what your saying. The second call is clearly a string. yes your example works, I think problem is when there is a non ASCII character in the text or buffer. – koolaang Dec 09 '19 at 17:29

1 Answers1

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

The GTMD
  • 148
  • 12
  • 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? – koolaang Dec 10 '19 at 23:19
  • 1
    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. – The GTMD Dec 11 '19 at 07:45