on 2020 Jul 07 10:50 AM
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?
Request clarification before answering.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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);
});
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!
Hi ,
I'm getting CORS error in ajax post call. Can you suggest any solution for this ?
My ajax post call code ->
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.
User | Count |
---|---|
86 | |
11 | |
8 | |
8 | |
6 | |
6 | |
5 | |
5 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.