Skip to content

Instantly share code, notes, and snippets.

@goFrendiAsgard
Created September 18, 2019 06:40
Show Gist options
  • Save goFrendiAsgard/754bf9b53deb579c6e5baa62c9b3c0b7 to your computer and use it in GitHub Desktop.
Save goFrendiAsgard/754bf9b53deb579c6e5baa62c9b3c0b7 to your computer and use it in GitHub Desktop.
force download
package main
import (
"fmt"
"io"
"net/http"
)
func serve(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Transfer-Encoding", "Binary")
w.Header().Set("Content-disposition", "attachment; filename=\"whatever.json\"")
_, err := io.WriteString(w, "{\"name\": \"Patrick Nindyatama\"}")
if err != nil {
fmt.Println(err)
}
}
func main() {
forever := make(chan bool)
http.HandleFunc("/", serve)
go http.ListenAndServe(":8080", nil)
<-forever
}
@goFrendiAsgard
Copy link
Author

How to use:

  • Run go run main.go
  • Open http://localhost:8080
  • Expect to download whatever.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment