Base64 encoded string in JSON

1
IMHO the documentation on using filedocuments in REST services could be improved to say it mildly. I am trying to transport one filedocument with metadata fields through REST services between two Mendix environments. My first option was to base64 encode the file. I have created an export mapping. The JSON that comes out of it works when I put it in postman and send it over. I had some problems because when I base64 encode the file it contains characters that are not allowed in JSON so I had to URLencode the base64 string. The other side can decode it first before doing the base64 decode so that hurdle is taken. But when I am trying to post that JSON the Mendix server console gives me the error below. What am I missing here? Before the POST action I do the exact same export mapping and the resulting JSON is correctly handed by the other side. Or should I create the base64 string in a different manner? Response content for POST request to http://somerestservice HTTP/1.1 400 Bad Request Date: Mon, 15 Jun 2020 10:31:07 GMT Cache-Control: no-store Content-Type: application/json;charset=utf-8 Content-Length: 67 {"error":{"code":400,"message":"Unsupported Content-Type header."}} I also tried exposing the object that inherits from filedocument. So on the receiving end all is created by Mendix. But the body parameter that is received is missing all the metadata information. So I am either doing something wrong on the sending side with the content type or something else goes wrong. Unfortunately good examples are missing on ( https://docs.mendix.com/refguide/call-rest-action ) the documentation here on how to use custom requests with mixed content containing binary. Regards, Ronald [EDIT] When I use an object that inherits from filedocuments I just can not get it to work. It must be something with the mixed content type that is going wrong. I fixed it by removing this inheritence and only use base64 encoding to transport the binary content. If anybody has an JSON example mapping to mix JSON objects and filedocument objects I really would like to see those examples. And I removed the URL encoding. That seems not necessary. At least the test worked with with some batches I tested.
asked
4 answers
3

Hello Ronald,

have you set the contentType to the type you expeted (eg. json)?

answered
2

I'm sorry repeating Gerrit; but did you really send the Content-Type header on the consuming side, like shown below? Postman / SoapUI will automatically add this header, that's why. And if so: could you show the Trace/Debug-logs of the consuming side to validate what is send is correct JSON?

 

[EDIT 17-06]

I agree with Michiel, exporting and importing to XML-type can be easily done in Mendix, both base64-encoded strings as the Contents of FileDocuments are accepted.

<xs:restriction base="xs:base64Binary">

In JSON, you are “stuck” with just a String and you have to Base64 encode/decode it yourself.

On the other hand, I always prefer non-persistent entities in all my integrations; so I never use persistent FileDocuments in my mappings and determine when the base64 encoding/decoding takes place. 

answered
2

You should be able to support mixed data including files using a Publish REST Service using form parameters. The screenshots demonstrate passing text, JSON text, and binary files in one request.

You can generate the request in Postman. See screenshot for configuration. 

I recommend using the executable ngrok and the command ‘./ngrok http 8080’ to allow receiving POST requests while debugging the local project. 
 


POST operation (no path) with 4 Form parameters. 2 Strings, 1 FileDocument, 1 specialization of FileDocument. Match Name values in Postman request and map MPReceive microflow parameters.


Postman POST request, Default headers, Body > form-data.


ReceiveMP microflow

 

answered
0

I wouldn’t base64 encode the file. Just post it in binary format.

If you want to add metadata, I suggest to use HTTP headers.

answered