0

I am trying to post binary data using the code below :

let headers = new Headers()
headers.append('Content-Type', 'application/json')

this.http.post("http://localhost:8080/api/load",
    requestBytes,
    {
      headers: headers
    }).subscribe((data) => {
        console.log("success!!!")
    })

requestBytes is of the type Uint8Array

The request payload looks like this

{
   "0": 10,
   "1": 1,
   "2": 49
}

As you can see this appears to be a dict while I was anticipating it to be a byte array. This causes confusion on the server.

I tried changing the Content-Type to application/octet-stream and passing in the buffer instead of array but that didn't help too. The payload in that case is coming out to be empty.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
Ninja420
  • 3,542
  • 3
  • 22
  • 34

1 Answers1

0

Had to manually create the request using XMLHttpRequest.

The final code looks something like this :

const httpRequest = new XMLHttpRequest()
httpRequest.open('POST', 'http://localhost:8080/api/restaurant/load', true)
httpRequest.responseType = "arraybuffer"
httpRequest.send(requestBytes)
Ninja420
  • 3,542
  • 3
  • 22
  • 34