Directories

Directories are the second type of template resource. They are collections of files and directories resources.

Here is service’s API overview:

  • PUT: directories are automatically created when you PUT a file in it. There is currently no way to create a whole directory with one request (uploading an archive as an example).
  • GET: list files in directory.
  • POST: render templates in directory against context, as an archive.
  • other HTTP verbs aren’t implemented yet.

Note

In the examples below, we’ll consider that a diecutter service is running at http://localhost:8106.

PUT

Let’s create some “greetings” directory with “hello.txt” and “goodbye.txt” inside:

$ echo "Hello {{ name }}!" | curl -X PUT  -F "file=@-" http://localhost:8106/greetings/hello.txt
{"diecutter": "Ok"}

$ echo "Good bye {{ name }}!" | curl -X PUT  -F "file=@-" http://localhost:8106/greetings/goodbye.txt
{"diecutter": "Ok"}

GET

GET lists files in directory:

$ curl http://localhost:8106/greetings/
hello.txt
goodbye.txt

Notice that, if you don’t set the trailing slash, you get the same list with folder name as prefix:

$ curl http://localhost:8106/greetings
greetings/hello.txt
greetings/goodbye.txt

POST

POST renders directory against a context:

$ curl -X POST -d name="world" -s http://localhost:8106/greetings | tar -zx

$ tree greetings
greetings
├── hello.txt
└── goodbye.txt
0 directory, 2 files

$ cat greetings/hello.txt
Hello world!

$ cat greetings/goodbye.txt
Goodbye world!

All files in directory are rendered with the same context data.

Like with GET, the trailing slash affects filenames: without trailing slash, filenames are prefixed with directory name.

Supported archives types: “accept” header

By default, POST requests return GZIP archives.

You can get the content as a TAR.GZ archive with the “accept” header:

$ curl -X POST --header "accept:application/zip" -d name="world" http://localhost:8106/greetings > greetings.zip
$ unzip greetings.zip
$ tree greetings
greetings
├── hello.txt
└── goodbye.txt
0 directory, 2 files

Tip

You can see all supported “accept” headers by requesting an unknown mime type:

$ curl -X POST --header "accept:fake/mime-type" -d name="world" http://localhost:8106/greetings
406 Not Acceptable

The server could not comply with the request since it is either malformed or
otherwise incorrect.


Supported mime types: */*, application/gzip, application/x-gzip,
application/zip