cancel
Showing results for 
Search instead for 
Did you mean: 

Document Information Extraction - SAPUI5 Ajax POST call

ramesh_yuvashree
Explorer
1,876

I am trying to post a pdf to the "document-information-extraction/v1/document/jobs" for processing it and extracting texts. The post call is something like this -

var data1 = new FormData();

this.onuploadFile is the pdf file.

data1.append("file", this.onuploadFile,"mypdf"); data1.append("options", "{\"extraction\":{\"headerFields\":[\"documentNumber\",\"taxId\",\"taxName\",\"purchaseOrderNumber\",\"shippingAmount\",\"netAmount\",\"senderAddress\",\"senderName\",\"grossAmount\",\"currencyCode\",\"receiverContact\",\"documentDate\",\"taxAmount\",\"taxRate\",\"receiverName\",\"receiverAddress\"],\"lineItemFields\":[\"description\",\"netAmount\",\"quantity\",\"unitPrice\",\"materialNumber\"]},\"clientId\":\"c_00\",\"documentType\":\"invoice\",\"enrichment\":{\"sender\":{\"top\":5,\"type\":\"businessEntity\",\"subtype\":\"supplier\"},\"employee\":{\"type\":\"employee\"}}}");

var settings = { "url": "/DIC_trial_API/document/jobs", "method": "POST", "timeout": 0, "headers": {"Authorization": "Bearer "+bearerToken }, "processData": false, "mimeType": "multipart/form-data", "contentType": false, "data": data1 };

$.ajax(settings).done(function (response) { console.log(response); });

The response is "400 Error", with message as "{"error": {"code": "4", "message": "Required form-data not provided.", "details": [{"code": "0", "message": "options"}]}}".

Can anyone help if there's anything wrong with the formdata or post call method?

View Entire Topic
tobias_weller
Product and Topic Expert
Product and Topic Expert

Hi Ramesh,

Can you try stringifying the JSON before adding it to the form?

The following code works fine here:

 var form = new FormData($('#fileinfo')[0]);
                form.append('file', $('input[type=file]')[0].files[0]);

                var options = {
                    "extraction": {
                        "headerFields": [
                            "taxId",
                            "taxName",
                            "purchaseOrderNumber",
                            "shippingAmount",
                            "netAmount",
                            "senderAddress",
                            "senderName",
                            "grossAmount",
                            "currencyCode",
                            "receiverContact",
                            "taxAmount",
                            "taxRate",
                            "receiverName",
                            "receiverAddress",
                            "deliveryDate",
                            "paymentTerms",
                            "senderBankAccount"
                        ],
                        "lineItemFields": [
                            "description",
                            "netAmount",
                            "quantity",
                            "unitPrice",
                            "materialNumber"
                        ]
                    },
                    "clientId": "c_00",
                    "documentType": "invoice",
                    "receivedDate": "2020-02-17",
                    "enrichment": {
                        "sender": {
                            "top": 5,
                            "type": "businessEntity",
                            "subtype": "supplier"
                        },
                        "employee": {
                            "type": "employee"
                        }
                    }
                };

                stringified_options = JSON.stringify(options)
                console.log(stringified_options)

                form.append("options", stringified_options);

                var settings = {
                    "url": "http://XXXXXXXXX/document-information-extraction/v1/document/jobs",
                    "method": "POST",
                    "timeout": 0,
                    "headers": {
                        "Authorization": "Bearer {{token}}"
                    },
                    "processData": false,
                    "mimeType": "multipart/form-data",
                    "contentType": false,
                    "data": form
                };

                $.ajax(settings).done(function (response) {
                    console.log(response);
                });


 

Best regards
Tobias

ramesh_yuvashree
Explorer
0 Kudos

Hi Tobias,

Thanks for the code. So, the form element of Fileuploader is used in this case while creating a formdata. I am using a fileuploader and navigator.camera/image control to capture pdf and image respectively. The formdata looks like -

But, the content type still goes as "text/plain".

	var data1 = new FormData($('#container-cordova---View1--fileUploader-fu_form')[0]);
				data1.append("file", $('input[type=file]')[0].files[0]);
					data1.append("options", stringified);
	var settings = {
				"method": 'POST',
				"url": "/DIC_trial_API/document/jobs",
				"timeout": 0,
				"headers": {
	"Authorization": "Bearer " + bearerToken
				},
				"contentType": false,
                                "data": data1,
                                "mimeType": "multipart/form-data",
				"processData": false
			};
			$.ajax(settings).done(function (response) {
				console.log(response);
			});
harianantha
Participant
0 Kudos

Hi Tobias,

I am getting CORS issue when I use the direct AI swagger uri by using xmlHTTPRequest. How to solve this? and Can you please help me in creating a destination the sap BTP for this AI Dox service? I want to use the destination in order to use this DOX service in the SAP MDK app.

Thank you so much!

tobias_weller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Harikrishna,

Could you create an incident on component CA-ML-BDP? It's tricky to analyze this issue here.

Best,
Tobias

0 Kudos

Hi ,

I'm getting CORS error in ajax post call. Can you suggest any solution for this ? 

My ajax post call code ->

  $.ajax({
                    method: "POST",
                    timeout: 0,
                    headers: {
                        Authorization: `Bearer ${token}`
                    },
                    processData: false,
                    mimeType: "multipart/form-data",
                    contentType: false,
                    data: oFormData,
                    success: function (response) {
                        console.log(response);
                    },
                    // Error handling
                    error: function (error) {
                        console.log(error);
                    }
                });
 
chandurkar_prachi_0-1742536617993.png

I configured my destination with help of service key of instance of document extraction.

My Api url in destination is -> https://aiservices-trial-dox.cfapps.us10.hana.ondemand.com/document-information-extraction/v1/

Is is correct format of API url or should I apply https://aiservices-trial-dox.cfapps.us10.hana.ondemand.com/document-information-extraction/v1/swagge... this as api url ?

Thanks in advance.