Created
September 18, 2019 06:40
-
-
Save goFrendiAsgard/754bf9b53deb579c6e5baa62c9b3c0b7 to your computer and use it in GitHub Desktop.
force download
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
go run main.go
http://localhost:8080
whatever.json