Skip to content

Instantly share code, notes, and snippets.

@debuglevel
Last active March 2, 2019 11:51
Show Gist options
  • Save debuglevel/16ca7a5538bb5bc097ada0497102545b to your computer and use it in GitHub Desktop.
Save debuglevel/16ca7a5538bb5bc097ada0497102545b to your computer and use it in GitHub Desktop.

incomplete and barely proven comparison of file transfer methods with REST

JSON with base64

$ curl -X POST -d @content.json -H "Content-Type: application/json" -H "Accept: application/json" http://localhost/
$ cat content.json
{  
   "files":{  
      "main.tex":"XGRvY3VtZW50Y2xhc3N7YXJ0aWNsZX0KXGJlZ2lue2RvY3VtZW50fQpcaW5jbHVkZXt0ZXN0L3Rlc3R9ClxlbmR7ZG9jdW1lbnR9",
      "test/test.tex":"aGFsbG8gd2VsdA=="
   }
}
  • One request for all files (might be easier handling on consumer side)
  • Increases file size by about 33% (👎 bad for big files, 🤷‍ does not matter much for small files and low traffic)
  • If POST transfers back a processed result, this might lead to a synchronous style which is not very REST-y. (But also might be acceptable, because it is easier; and does not matter if no scalability is required)
  • With a synchronous style, server can just delete all files after processing (or only retain the result file).

Multiple POST

$ curl -X POST -d @file1 -H http://localhost/files/
$ curl -X POST -d @file2 -H http://localhost/files/
  • Transfering each resource on its own is more REST-y
  • If file processing is wanted, an additional GET is needed (and probably some description file which points to all previously uploaded files).
  • If files are only needed temporarily, server has to handle some garbage collection.

Multipart

Content-Type: multipart/form-data; boundary=12345

--12345
Content-Disposition: form-data; name="sometext"

some text that you wrote in your html form ...
--12345
Content-Disposition: form-data; name="name_of_post_request" filename="filename.xyz"

content of filename.xyz that you upload in your form with input[type=file]
--12345
Content-Disposition: form-data; name="image" filename="picture_of_sunset.jpg"

content of picture_of_sunset.jpg ...
--12345--
  • Sometimes badly supported by libraries.
  • Not very intuitive and beautiful; rather error prone.
  • No idea if a path separator in filename would be legal.
  • A simple HTML page without JavaScript could be used to transfer data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment