Note: this post is about the ArangoDB 2.x series
Strings vs. binary data
To make it work anyway, binary data needs to be stored in a wrapper
object. I won’t go into details about ES6 typed arrays here, but will
Binary data in Foxx actions
A Foxx route that shall handle HTTP POST requests containing arbitrary
(binary) body in the request body should not use
reason is that
and this isn’t going to work with arbitrary binary data.
req.rawBodyBuffer() should be used. This will return the
request body inside a buffer. Here’s an example that stores the received
data in a file on the server.
1 2 3 4 5 6 7 8
This action can be invoked as follows if the app is mounted with name
curl -X POST http://localhost:8529/app/receive-binary --data-binary @filename
This will send the contents of the file
filename to the server. The Foxx
action will then store the received data as is in a file name
body in the
Returning binary data from a Foxx action is simple, too. Here’s a way that
returns the contents of the file named
body in the application’s directory:
1 2 3 4 5 6
It is also possible to return data from an arbitrary buffer:
1 2 3 4 5 6 7 8 9 10 11
I quickly put together an example application that shows how to handle arbitrary binary data in Foxx actions. The example app allows uploading files to the server. The server will then list these files and allows downloading them again.
The application has no CSS at all. Its only purpose is to demo the server-side code. The application can be downloaded here. An older version of the application (compatible with 2.3) can be found here.
Please note that the example application requires ArangoDB 2.3, which is currently in development.