Skip to content

Commit 5723151

Browse files
authored
Merge pull request #23 from itowlson/header-meddling
Alter response headers example
2 parents 8b3f8e2 + ed9002f commit 5723151

11 files changed

+3412
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [A/B Testing using Cookies](./samples/ab-testing-cookie/)
1414
- [A/B Testing using Key-Value Store](./samples/ab-testing-kv/)
1515
- [A/B Testing using HTTP Headers (e.g User Agent)](./samples/ab-testing-user-agent/)
16+
- [Alter Response Headers](./samples/alter-headers/)
1617
- [Auto-Complete API](./samples/auto-complete/e/)
1718
- [Traffic Filtering: Block By IP address](./samples/block-by-ip/)
1819
- [Traffic Filtering: Block By user country](./samples/block-by-country/)

samples/alter-headers/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
dist
3+
target
4+
.spin/
5+
build/

samples/alter-headers/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
KNITWIT_SOURCE=./config/knitwit.json

samples/alter-headers/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Altering response headers
2+
3+
This sample shows how to alter the headers as you stream a response from an origin
4+
server back to a client.
5+
6+
You can set the origin site via the `origin_host` variable, either by editing `spin.toml`
7+
or by overriding it on the command line:
8+
9+
```
10+
SPIN_VARIABLE_ORIGIN_HOST=example.com spin up --build
11+
```
12+
13+
(Note this should _not_ include the `https://` prefix - just the host name.)
14+
15+
The path on the origin site is taken unchanged from the request.
16+
17+
## Try it out
18+
19+
The sample defaults to a "random facts" origin site. To try it out, `cd` into the
20+
`alter-headers` directory and run `spin up --build`. Then you can `curl` the
21+
`/animals/json` route, and use the `-i` option to see the altered response headers:
22+
23+
```
24+
$ curl -i localhost:3000/animals/json
25+
HTTP/1.1 200 OK
26+
content-length: 77
27+
date: the eleventy-sixth of June
28+
friendly-message: Hello from FWF
29+
30+
{"timestamp":1740943712057,"fact":"Sharks lay the biggest eggs in the world"}
31+
```
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": 1,
3+
"project": {
4+
"worlds": [
5+
"spin-http"
6+
]
7+
},
8+
"packages": {
9+
"@fermyon/spin-sdk": {
10+
"witPath": "../../bin/wit",
11+
"world": "spin-imports"
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)