Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Network Request Error: for Post Call from Build apps to Document information extraction service

Jagadeesh21
Explorer
0 Likes
1,535

I'm trying to Take Picture in Build apps and send image via POST Call to Document information Extraction service using JS workflow function.
1. as Destinations Does not support Multipart content as mentioned in the Issue with SAP BTP Destination Rest API Integratio... - SAP Community 

2. with reference to PDF Sending to REST API with JavaScript in SAP Bui... - SAP CommunityExplore and Infuse DOX capabilities in SAP BTP cus... - SAP Community
I have written the Java script : 

try {
// Declare and initialize variables inside the try block
let url = inputs.url;
let file = inputs.file;
let Name = inputs.imageName;
let Token = inputs.Token;
let imPath = inputs.imagePath
// Fetch the image blob from the imagePath
let iPath = await fetch(imPath);
let blob = await iPath.blob();

// Prepare the form data
const formData = new FormData();
formData.append('file', blob, Name);

// Define the options for the request
var lv_options = {
"clientId": "default",
"documentType": "custom",
"schemaName": "Danfoss"
};
formData.append('options', JSON.stringify(lv_options));

// Make the POST request
const response = await fetch(url, {
method: 'POST',
headers: {
Accept: "application/json",
"X-Requested-With": "XMLHttpRequest",
Authorization: "Bearer " + Token
},
body: formData
});

if (response.ok) {
let data = await response.json();
return { id: data.id };
} else {
// Handle HTTP errors
alert("HTTP-Error: " + response.status);
}
} catch (error) {
// Handle errors that occur during fetch or JSON parsing
console.error("Error occurred:", error);
alert("An error occurred: " + error.message);
}

3. Also Tried the Approach of Pick files from Build apps and replicated as it is mentioned in  PDF Sending to REST API with JavaScript in SAP Bui... - SAP Community

But still i'm Getting the Network Request Failed error. i don't know where i'm going wrong
Can anyone Please help me with this.
Thanks in Advance.

@Dan_Wroblewski @EvgeniyGorbunov 

I'm trying to Take Picture in Build apps and send image via POST Call to Document information Extraction service using JS workflow function.
1. as Destinations Does not support Multipart content as mentioned in the Issue with SAP BTP Destination Rest API Integratio... - SAP Community 

2. with reference to PDF Sending to REST API with JavaScript in SAP Bui... - SAP CommunityExplore and Infuse DOX capabilities in SAP BTP cus... - SAP Community
I have written the Java script : 

try {
// Declare and initialize variables inside the try block
let url = inputs.url;
let file = inputs.file;
let Name = inputs.imageName;
let Token = inputs.Token;
let imPath = inputs.imagePath
// Fetch the image blob from the imagePath
let iPath = await fetch(imPath);
let blob = await iPath.blob();

// Prepare the form data
const formData = new FormData();
formData.append('file', blob, Name);

// Define the options for the request
var lv_options = {
"clientId": "default",
"documentType": "custom",
"schemaName": "Danfoss"
};
formData.append('options', JSON.stringify(lv_options));

// Make the POST request
const response = await fetch(url, {
method: 'POST',
headers: {
Accept: "application/json",
"X-Requested-With": "XMLHttpRequest",
Authorization: "Bearer " + Token
},
body: formData
});

if (response.ok) {
let data = await response.json();
return { id: data.id };
} else {
// Handle HTTP errors
alert("HTTP-Error: " + response.status);
}
} catch (error) {
// Handle errors that occur during fetch or JSON parsing
console.error("Error occurred:", error);
alert("An error occurred: " + error.message);
}

3. Also Tried the Approach of Pick files from Build apps and replicated as it is mentioned in  PDF Sending to REST API with JavaScript in SAP Bui... - SAP Community

But still i'm Getting the Network Request Failed error. i don't know where i'm going wrong
Can anyone Please help me with this.
Thanks in Advance.

@Dan_Wroblewski @EvgeniyGorbunov 

2 REPLIES 2
Read only

Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Likes
1,474

It would help to see the errors you get, in console, response from API.

When I send to DMS, which I am assuming is similar, I do it slightly differently, and don;t do all the blob stuff.

const {url, fileObj, token} = inputs

try {
  const formData = new FormData()

  const fileData = {
    uri: fileObj.path,
    size: fileObj.size,
    type: fileObj.mimeType,
    name: fileObj.name
  }
  formData.append('media', fileData)
  formData.append('cmisaction', 'createDocument')
  formData.append('propertyId[0]', 'cmis:objectTypeId')
  formData.append('propertyValue[0]', 'cmis:document')
  formData.append('propertyId[1]', 'cmis:name')
  formData.append('propertyValue[1]', fileObj.name)
  formData.append('succinct', true)
  formData.append('filename', fileObj.name)
  formData.append('includeAllowableActions', true)

  //------

  const response2 = await fetch(url, {
    method: "POST",

    headers: {
      "Authorization": "Bearer " + token,
      "Accept": "application/json",
      "DataServiceVersion": "2.0",
      "Content-Type": "multipart/form-data"
    },
    body: formData
  })
  return {id: response2}
  
} catch (error) {
  console.log(JSON.stringify(error))
  const errorObj = {
    code: "9",
    message: "something went wrong",
    rawError: error
  }
  return [1, errorObj]
}

The fileObj is just the output of the pick files flow function: 

outputs["Pick files"].files[0]

Don't know if this would work for you.

 

 

 




--------------
See all my blogs and connect with me on Twitter / LinkedIn
Read only

Sankara1
Product and Topic Expert
Product and Topic Expert
0 Likes
1,363

Hello Jagdeesh,

Thank you for reaching out to us. Are you still encountering the issue? Did you have a chance to share the screenshot of the console error (response from the API)?