0

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);
},
gsubiran
  • 2,012
  • 1
  • 22
  • 33
Kumaran L
  • 1
  • 1
  • May I know how is the raw (data) format is as ? – KcH May 10 '20 at 09:24
  • raw output is something like "data:application/pdf;base64,JVBERi0xLjMNCiXi48/TDQolUlN...." . I tried to post using "data:application/pdf;base64,JVBERi entire string and also by removing data:application/pdf;base64. In both cases the error is same. Also, I am able to generate PDF using the raw data in a "Base64 to PDF" weblink. – Kumaran L May 10 '20 at 10:17

1 Answers1

0

You could use atob javascript function to decode the Base64 string (link)

Example:

    //plain text base64 WITHOUT datacontent and other stuff
    let base64string = "JVBERi0xLjQKJ..."
    
    let byteCharacters = atob(base64string);
    
    jQuery.ajax({
            type    : "POST",
            url     : "<form recognizer url endpoint>",
            context : this, 
            crossDomain: true,
            data: byteCharacters,
            beforeSend: function(xhr) {
    
                xhr.setRequestHeader("ocp-apim-subscription-key", "<your_key>");
    
                //To avoid specify pdf or image type use octet-stream
                xhr.setRequestHeader("content-type", "application/octet-stream");            
    
            },
            error   : function(jqXHR, textStatus, errorThrown) {
                console.error(errorThrown);
                },
            success : function(oData, status, jqXHR) {
                console.info(status);
            }
    });

To get plain base64 string from pdf you could use this website to test: https://base64.guru/converter/encode/pdf

gsubiran
  • 2,012
  • 1
  • 22
  • 33