Skip to content Skip to sidebar Skip to footer

How To Send Image Using Application/octet-stream In OCR Cognitive

Hello I'm trying to use OCR API From Microsoft and It expect Content-type application/octet-stream and body post a binary. I tried send image as Base64(binary), just binary, howeve

Solution 1:

Yes, you can simply send it as a Blob or a File (which are almost the same things).

Example code using the XMLHttpRequest API :

var xhr = new XMLHttpRequest();
xhr.onload = do_something_with_this_JSON;
xhr.open('POST', 'https://westus.api.cognitive.microsoft.com/vision/v1.0/ocr');
xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.setRequestHeader("Ocp-Apim-Subscription-Key", YOUR_KEY);
xhr.send(blob);

Now on how to get a Blob, this really depends on where you get your image from.

  • if it comes from an <input type="file">, then you can send it like that.
  • if it comes from a request (then why don't you send the url as application/JSON?) you can request the response to be a blob (xhr.responseType = "blob" or fetch().then(resp => resp.blob()).
  • if you've got a canvas, then you can use its toBlob method.
  • if you only have a dataURI, then check this Q/A.

Post a Comment for "How To Send Image Using Application/octet-stream In OCR Cognitive"