0

I'm attempting to use an API (there is no documentation), the backend uses ScalaJS and uPickle (MsgPack) and it returns raw binary data for each API endpoint. I need to somehow covert this raw binary data to readable JSON.

Here is my code:

function arrayBufferToString( buffer, encoding, callback ) {
    var blob = new Blob([buffer],{type:'text/plain'});
    var reader = new FileReader();
    reader.onload = function(evt){callback(evt.target.result);};
    reader.readAsText(blob, encoding);
}
fetch("https://example.com/example/fetchStats", {
    "credentials": "include",
    "headers": {
        "accept": "*/*",
        "accept-language": "en-US,en;q=0.9",
        "content-type": "application/octet-stream"
    },
    "body":"\u0000",
    "method":"POST",
    "mode":"cors"
}).then(response => response.arrayBuffer().then(arrayBuffer => {
    if (arrayBuffer) {
        var bytes = new Uint8Array(arrayBuffer);
        var u16 = new Uint16Array(bytes.length)
        for(var i=0;i<bytes.length;i++){
            u16[i] = bytes[i].toString().charCodeAt(0)
        }
        arrayBufferToString(u16, 'UTF-8', console.log.bind(console))

    }
}));

This returns the following string: "3011102010301" (with spaces between the numbers) I am not sure how to read this data. Like I said above, the API uses this https://github.com/lihaoyi/upickle in place of JSON.

Does anyone know how to convert the raw binary data to a human readable format?

Sample of what the HTTP request above would return: ╚ ╔t╔ ╗ ╔ ╚ ╔

I'm so lost.

1 Answers1

0

Can't you use response.text() or response.json() instead of response.arrayBuffer()

fetch('https://httpbin.org/get')
  .then(response => response.json())
  .then(console.log);
Endless
  • 34,080
  • 13
  • 108
  • 131