cancel
Showing results for 
Search instead for 
Did you mean: 

Calling Microsoft Cognitive REST API in SAPUI5

Kumaranl
Explorer
0 Kudos

I am trying to call the form-recognizer API using SAPUI5 (Jquery / AJAX) post method. I am able to read the same pdf using RESTAPI client. The API when called from Javascript gives the below error.

The issue seems to be around data in the body of the ajax post method. Any suggestion/help is highly appreciated.

Error Message : 415 Unsupported Media Type {"error":{"code":"2018","innerError":{"requestId":"a12dc9f8-b22f-4602-85d8-7330b16593f7"},"message":"Content parsing error."}}

Javascript code :

  onChange: function(oEvent) {
  //  var that = this;
    var reader = new FileReader();
    var file = oEvent.getParameter("files")[0];
    var raw;

    reader.onload = function(e) {
        raw = e.target.result;
        //alert(raw);
        var sUrl2 = "https://formrecognizerforsap.cognitiveservices.azure.com/formrecognizer/v1.0-preview/custom/models/{mymodelid>/analyze";

    jQuery.ajax({
        type    : "POST",
        url     : sUrl2,
        context : this, 
        crossDomain: true,
        data: raw,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("content-type", "application/pdf");

            xhr.setRequestHeader("ocp-apim-subscription-key", "my-subscription id");
        },
        error   : function(jqXHR, textStatus, errorThrown) {
            sap.m.MessageToast.show(errorThrown);
            },
        success : function(oData, status, jqXHR) {
    sap.m.MessageToast.show(status);
        }

        }); 
     };

    reader.onerror = function(e) {
        sap.m.MessageToast.show("error");
    };
    reader.readAsDataURL(file); 

    },

s

Accepted Solutions (0)

Answers (0)