Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ including:
webtools.SetContentType(w, webtools.ContentTypeRSS)
```

- `Content-Disposition`
```go
webtools.SetAttachment(w, "note.txt")
```

- `X-Robots-Tag`

```go
Expand Down
8 changes: 8 additions & 0 deletions headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ func SetContentType(w http.ResponseWriter, filetype MIMEType) {
w.Header().Add("Content-Type", string(filetype))
}

// SetAttachment sets the Content-Disposition header on w to the given
// filename string. Use this in conjunction with SetContentType to create
// a downloadable file in an HTTP response.
func SetAttachment(w http.ResponseWriter, filename string) {
value := fmt.Sprintf("attachment; filename=%q", filename)
w.Header().Set("Content-Disposition", value)
}

// RobotIndex are correct sentinel values for indicating whether a page
// should be indexed, as set in the X-Robots-Tag HTTP response header.
//
Expand Down
8 changes: 8 additions & 0 deletions headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func Test_SetContentType(t *testing.T) {
must.Eq(t, "text/plain; charset=utf8", w.Header().Get("Content-Type"))
}

func Test_SetAttachment(t *testing.T) {
t.Parallel()

w := httptest.NewRecorder()
SetAttachment(w, "note.txt")
must.Eq(t, `attachment; filename="note.txt"`, w.Header().Get("Content-Disposition"))
}

func Test_SetBasicAuth(t *testing.T) {
t.Parallel()

Expand Down