On this page
Answer File
HTTPE can serve files from disk. Optionally these files are send through the template engine to do on-the-fly manipulation.
Example
---
rules:
- name: GET File 1
on:
path: /file/1
methods:
- get
answer.file: /etc/hosts
- name: GET File 2
on:
path: /file/2
methods:
- get
answer.file: /tmp/test.txt
args:
template: true
respond:
on_success:
headers:
My-Header: Super Dupa
on_error:
headers:
My-Header: THIS IS AN ERROR
- name: Music
on:
path: /file/3
methods:
- get
answer.file: /tmp/music.mp3
args:
templating: false
respond:
on_success:
headers:
Content-Type: audio/mpeg
Content-Disposition: filename="music.mp3"
To see it in action, create a file /tmp/test.txt
with the following content:
Hello {{ .Input.Params.Name }}!
How are you today?
Then fire some requests.
$ curl localhost:3000/file/1
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
$ curl localhost:3000/file/2?Name=John
Hello John!
How are you today?
When serving files from disk, templating is turned off by default. Read more about templating.
All files are read into memory before sending the response even if templating is tuned off.
Consider using serve.directory instead which implements a static file server that serves
files from disk.
File Paths
On Windows you can do both, a backslash or a forward slash to specify a file path. The below examples are both valid.
- name: Hosts
on:
path: /hosts
#answer.file: C:\Windows\System32\drivers\etc\hosts
answer.file: C:/Windows/System32/drivers/etc/hosts
Paths containing blank spaces doesn’t require escaping or quoting. Just type it in with blank space such as
answer.file: C:/Users/thorsten/httpe_0.0.1_Windows_x86_64/an important folder/file.txt