Object Headless Api Upload File

Hello, I need to create a React portlet that invokes the headless APIs of an Object to create an object entry by entering a series of data, including uploading a file.

To upload a file, do I first need to invoke the headless APIs to load the file into the DML? Does anyone have an example or a list of APIs that I should invoke to achieve my goal?

https://learn.liferay.com/w/dxp/integration/headless-apis/object-apis/custom-object-apis/using-object-apis-with-base64-encoded-files#posting-an-attachment-uploaded-directly-from-the-users-computer

Can we specify a folder in the POST payload in which upload the file ONLY for object fields with "Upload or Select from Documents and Media Item Selector" option? @ChrisHof

Yes, you just have to check the OpenAPI definition in API Explorer for your Object for all the fields in the request body...

For attachment fields in the schema I see only "{}", so I dont know which parameters I Need to send...

1 Answer

1

Hi @Moozo,

There are two ways to attach the files, the first one is upload the file in the documents & media portlet, and then pass the id of the file in the body, like below:

{

"name": "EXAMPLE01",  
"attachmentField": 34050 // this is fileentryid, and can be obtained from dlfileentry table

}

The second is to upload the file using base64 encoding, and for that, you can refer to the following request body:

{
“attachment” : {
“name” : “file.png”,
“fileBase64” : “iVBORw0KGgoAAAANSUhEUgAAAD0AAAAXCAIAAAA3N9DuAAAAA3NCSVQICAjb4U/gAAAAEHRFWHRTb2Z0d2FyZQBTaHV0dGVyY4LQCQAABX9JREFUWMOVWFuS3EgOA8BUlXpifuYee4u9/3nWdonEfDAzS91+RKw+HOooKUmCAEiZ//3nP7ZJ4jeX7XR+v15VBizSgO2Q+iarAIoMMcv9FgkbBAyMoI0s7yD9EwkDNiJYZRshAihb69Eqg+igJLheV7k66XKVqxPtd668Ml8kg3GOI0QDaZRB8qrKLBtlhEjilSXO2B24k77SWRbRvxIQYVjiDGV4HbLfLTurIkiybAAE+qiyh6jOUlRnTLI7MGIs8Bgc58C36+XGwBhS2SGGomEWmeURFJHlhr9DShB5pUmTrLKkKneRIEIsG4bIxluk55M03ijMbAFU1c7vfpOZmyrllMZfj49DDFFilm280k2ARqtrLlti2VfWBK9w5aytyiJg9yEkYPRpNsiJ9ysL8xxwVtcs8JUWAElfON0ZR8QuQ5z3j3Gex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Y1A0wHfHttxehXZnrNJomDvFK8srR2ogkpXCz+rdurBaLcu23p82VUIhiKoPxNG0DnOj0d01LTJxeP+7tLien9kiHMuqpcldltG0Pb5CO0RaDT+L4G9nzRtyiYIWGtO3SVoOKvSRXDb7c9jyzDJ8/h4jngewcasU19LIoCmafvCVuf+eCNxpY+hKy3DdpGCC2Dx0V0biqtyB+51oNuy8rm2LbK+++bsWQnnZyFMLzri+RzPkHr1bZb3l47IH9ccJFWG3Z8dNvZe1VwfncS0IApoRZfITjp9I7dNMF2ihsKMTRjrKdLvOSDwq15FNQSiPo5nEPu7rtlse4i5uFHzvwB4rc60b/aU/Rc6sWizbSKbGQAAAABJRU5ErkJggg==”

  }

}

Please note that the content type is not form-data, so you need to choose ‘raw’ body type and send the data as JSON.

For more reference: Upload attachments to custom objects Headless API - Liferay Learn