0

I need to send video from disk to Cognitive Services Emotion. I have almost done, but I was not capable to figure out how to format body properly and I can´t use '{url: http://...}' because I can´t expose the videos I want to submit. My code:

$.ajax({
            "headers":{
                "Content-Type":"application/octet-stream",
                "Ocp-Apim-Subscription-Key":"SECRET"
            },        
            type: "POST",
            url: url,
            "data": JSON.stringify({video: data}),
            success: (x,stat,res) => {
            },
            error: (res) => {
            }
        });

As you can see I´ve tried to use 'octet-stream' to send the video. And that is one of many ways I did it. I don´t know if I need to send a JSON(similar when you send url) or do something else. I couldn´t find anything about it on official documentation. Help??

Thanks!

Andre Carneiro
  • 708
  • 1
  • 5
  • 27

1 Answers1

0

Please take a look at Sending binary data in javascript over HTTP.

$.ajax({
            "headers":{
                "Content-Type":"application/octet-stream",
                "Ocp-Apim-Subscription-Key":"SECRET"
            },        
            type: "POST",
            url: url,
            data: data,
            success: (x,stat,res) => {
            },
            error: (res) => {
            }
        });

You want the body payload to be the raw (unencoded) binary data for Cognitive Services. The accepted formats are listed here.

One other important thing to remember is that jQuery calls the error handler for HTTP 202 responses, which is what you get from this API. So your error handler needs to handle that case, or, more to the point, your success handler won't ever be called.

cthrash
  • 2,938
  • 2
  • 11
  • 10
  • Yes, I read the documentation, thank you very much! There is no useful information about how to format data for videos nor examples. And the emulator doesn´t helps to find out how am I supposed to format the data. The article about how to send binary data in Javascript doesn´t helps too because problems are a little bit different. Trying to explain better, I´m using FileReader to read video from the disk using readAsDataURL(file) as "encoder". I did that because works for images. But is not that for upload videos and documentation about it is not clear for me. – Andre Carneiro Jun 23 '17 at 18:19
  • If you have a DataURI, you need to convert this to a binary buffer. [Post](https://social.msdn.microsoft.com/Forums/en-US/807ee18d-45e5-410b-a339-c8dcb3bfa25b/testing-project-oxford-ocr-how-to-use-a-local-file-in-base64-for-example?forum=mlapi) is for a different Microsoft API but the same will work for you. – cthrash Jun 23 '17 at 18:25