File upload in rest api using base64

Hello,

I am trying to add a new file (FileDescriptor) via the rest api. The content of the file is base64 encoded.
This curl command shows how we tried to upload the file (show work with an empty cuba application):


curl -X POST -H "Accept: application/json" -H "Authorization: Bearer 3781d35a-9c7f-4e91-a7e7-e36346424ea2" -H "Content-Type:  multipart/form-data; boundary=AaB03x" -H "Cache-Control: no-cache" -H "Postman-Token: b040447b-0acd-65b4-aad8-ddc0c528c115" -d '
--AaB03x
Content-Disposition: form-data; name="file"; filename="test64.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwYAAABNmlDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjarY6xSsNQFEDPi6LiUCsEcXB4kygotupgxqQtRRCs1SHJ1qShSmkSXl7VfoSjWwcXd7/AyVFwUPwC/0Bx6uAQIYODCJ7p3MPlcsGo2HWnYZRhEGvVbjrS9Xw5+8QMUwDQCbPUbrUOAOIkjvjB5ysC4HnTrjsN/sZ8mCoNTIDtbpSFICpA/0KnGsQYMIN+qkHcAaY6addAPAClXu4vQCnI/Q0oKdfzQXwAZs/1fDDmADPIfQUwdXSpAWpJOlJnvVMtq5ZlSbubBJE8HmU6GmRyPw4TlSaqo6MukP8HwGK+2G46cq1qWXvr/DOu58vc3o8QgFh6LFpBOFTn3yqMnd/n4sZ4GQ5vYXpStN0ruNmAheuirVahvAX34y/Axk/96FpPYgAAACBjSFJNAAB6JQAAgIMAAPn/AACA6AAAUggAARVYAAA6lwAAF2/XWh+QAAAFvklEQVR42tSay1NTVxzHPxcCxSSFkuiMQngl5sYpL8dOZ4rajuFhJQRct3Y6/gWdscvWZd3WVZfdWad2uuHlVAiPdqwunE4Ra00iCY9ERCSRwCUir9sFjwZz0zy4VPjOZCa5555zvp+T3znnd3IjzM3NsaFW4BLwPqBnb0oC7gHfAl0AwgbAN8DX7C9dAS4Lc3NzTqCT/am2LOB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JjVAzhiOMDo6Gjcac1e30CL05lZNrpZUatV3nltpnzycrMzykRjla/L5dN6M3/e7UvLfErpdIvTiSRJypWzs6gsL+QP70zapvNys6kqL+S4xcCH1YfJ1+ZwI2YCp2I+pXQaQKfTJdwjas2pAWQJAtbifGrMhdRaDNhMBWRn/buKSfPzTE5Obpivx9HSQireNDuN3Y+qD/Pzb2PMRZfjyoqMWmrNBmotBqrK30H7VuLuvBub17p5Z8r9C9PT02kd6u/euUMgEMAqWhGtIjq9niczUa4P+JkKv8R0UEuN2UCtpZCD+Xkpt/vTjR/R6/VpmU85hGL1QV0d4XCY69eubR0xraKVthqRiooacnJytyVqqcpkKqHu5EnS9SM8e/Ysox+2bnZ3MzjQH3fgKS8vxyqKWEUbxcXFqu7WqgIkgng9g7UcPYoo2rCKIgaDYW8BpAIRK4PRiNVqpaLCzAGtltKSEnR6/ZsFSBdiU2fs60vlTpX2JFZSs8MByAwODKRo3k6zw4Eafav2hKbZ0cIZuz1F8y2qzQFVn5E1O1oUc/bYfF5N8wDC1NSU6o9Z/374kMGBfgKBAIIgYDKZOGOv593KSrUXIUmYmprqB+zsTw1oZFm+uo8Brmax/j+JK/vQ/BWgc3MSXwbOAwPAwh42vbDhsW3DM/8MANPp6EOvptK2AAAAAElFTkSuQmCC
--AaB03x--' "http://localhost:8080/app/rest/v2/files"

The file is cuba is larger than the original file and it is not a valid image file. How can I upload the file with base64?
Thanks for your support.

Yours,
Joerg

We’ll think about implementing base 64 decoding. You may create an idea in the ‘Ideas’ section of the forum, so other people will be able to vote for this feature.

Hi Joerg,

File upload controller expects not encoded data, so automatic decoding from base 64 is not performed. You must send not encoded data or implement your own controller that will decode it.

Why are you sending base64 string? What client type do you use?

Hi Max,

thank you for you super fast reply.
In our setup, we are using the Kettle ETL-Engine to get data from a XML-file into the CUBA app. Some data is entity related but some data are PDF-files which have to be encoded to base64 to transfer them by XML. After reading about the possibility to use multipart/form-data for the file-controller I just assumed the decoding to work automatically.
In Kettle, there is a REST-exporter we use, but sadly there seems to be a bug when transfering binary data since the exporter converts the data to a string before sending.
Our solution so far is using a custom java snippet, in which we convert and send the data “manually” (without Kettle tools).
But I like your suggestion to implement our own controller for receiving encoded data. Is there any chance this will get implemented in CUBA at some point in the future? Receiving files via XML always raises this issue.

Thanks again!

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-8458