Skip to content

Commit

Permalink
feature: add example for http status 204 and 304
Browse files Browse the repository at this point in the history
  • Loading branch information
nanmu42 committed Sep 18, 2021
1 parent ac2e31b commit 2feb5f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions examples/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func main() {
})
})

g.GET("/204", func(c *gin.Context) {
c.Status(http.StatusNoContent)
})

g.GET("/304", func(c *gin.Context) {
c.Status(http.StatusNotModified)
})

const port = 3000

log.Printf("Service is litsenning on port %d...", port)
Expand Down
23 changes: 22 additions & 1 deletion examples/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ func main() {
writeString(w, fmt.Sprintf("This content is compressed: l%sng!", strings.Repeat("o", 1000)))
})

mux.HandleFunc("/204", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
})

mux.HandleFunc("/wrong204", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
writeString(w, fmt.Sprintf("This content is compressed: l%sng!", strings.Repeat("o", 1000)))
})

mux.HandleFunc("/304", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotModified)
})

mux.HandleFunc("/wrong304", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotModified)
writeString(w, fmt.Sprintf("This content is compressed: l%sng!", strings.Repeat("o", 1000)))
})

const port = 3001

log.Printf("Service is litsenning on port %d...", port)
Expand All @@ -30,5 +48,8 @@ func main() {

func writeString(w http.ResponseWriter, payload string) {
w.Header().Set("Content-Type", "text/plain; charset=utf8")
_, _ = io.WriteString(w, payload+"\n")
_, err := io.WriteString(w, payload+"\n")
if err != nil {
fmt.Printf("wrting body: %s\n", err)
}
}

0 comments on commit 2feb5f4

Please sign in to comment.