AngularJS: POST request payload empty for ArrayBuffer

1k views Asked by At

I tried to use POST to send some bytes using ArrayBuffer, but it seems that the request payload is always empty. Here's some code snippet:

var buffer = new ArrayBuffer(10);
var dataView = new DataView(buffer);
dataView.setInt32(0, 0x1234ABCD);
var request = {
  method: 'POST',
  url: 'url',
  headers: {'Content-Type': 'application/octet-stream'},
  data: buffer,
  responseType: 'arraybuffer'
};
$http(request)
  .success(function(data, status, headers, config) { ... })
  .error(function(data, status, headers, config) { ... });
1

There are 1 answers

0
Blazingspike On

As I commented, to send ArrayBuffer, you need to use XHR2 directly (seems AngularJs doesn't support it). Here's some code of how to do it:

var buffer = new ArrayBuffer(10);
var dataView = new DataView(buffer);
dataView.setInt32(0, 0x1234ABCD);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'url', true);
xhr.onload = function(e) {
  console.log('data received');
};
xhr.send(buffer);

More details about XHR2: http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-string, hope this could help for someone who's also puzzled :)