Skip to content

Commit 3c5ae6f

Browse files
committed
Add more examples for a custom return Header
Signed-off-by: Alex Ellis <[email protected]>
1 parent 122ec27 commit 3c5ae6f

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,29 @@ func Handle(req handler.Request) (handler.Response, error) {
8888
```
8989

9090
The error will be logged to `stderr` and the `body` will be written to the client along with a HTTP 500 status code.
91+
92+
Example reading a header.
93+
94+
```go
95+
package function
96+
97+
import (
98+
"log"
99+
100+
"github.com/openfaas-incubator/go-function-sdk"
101+
)
102+
103+
// Handle a function invocation
104+
func Handle(req handler.Request) (handler.Response, error) {
105+
var err error
106+
107+
log.Println(req.Header) // Check function logs for the request headers
108+
109+
return handler.Response{
110+
Body: []byte("This is the response"),
111+
Header: map[string][]string{
112+
"X-Served-By": []string{"My Awesome Function"},
113+
},
114+
}, err
115+
}
116+
```

template/golang-http/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ WORKDIR /home/app
3030

3131
COPY --from=0 /go/src/handler/handler .
3232
COPY --from=0 /usr/bin/fwatchdog .
33-
RUN apk add --no-cache curl
33+
# RUN apk add --no-cache curl
3434
USER app
3535

3636
ENV fprocess="./handler"

template/golang-http/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ func makeRequestHandler() func(http.ResponseWriter, *http.Request) {
3535

3636
result, resultErr := function.Handle(req)
3737

38+
if result.Header != nil {
39+
for k, v := range result.Header {
40+
w.Header()[k] = v
41+
}
42+
}
43+
3844
if resultErr != nil {
3945
log.Print(resultErr)
4046
w.WriteHeader(http.StatusInternalServerError)

0 commit comments

Comments
 (0)