Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send 204 No Content with Go http package?

I built a tiny sample app with Go on Google App Engine that sends string responses when different URLs are invoked. But how can I use Go's http package to send a 204 No Content response to clients?

package hello

import (
    "fmt"
    "net/http"
    "appengine"
    "appengine/memcache"
)

func init() {
    http.HandleFunc("/", hello)
    http.HandleFunc("/hits", showHits)
}

func hello(w http.ResponseWriter, r *http.Request) {
    name := r.Header.Get("name")
    fmt.Fprintf(w, "Hello %s!", name)
}

func showHits(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%d", hits(r))
}

func hits(r *http.Request) uint64 {
    c := appengine.NewContext(r)
    newValue, _ := memcache.Increment(c, "hits", 1, 0)
    return newValue
}
like image 853
Ingo Avatar asked Jul 22 '13 02:07

Ingo


1 Answers

According to the package docs:

func NoContent(w http.ResponseWriter, r *http.Request) {
  // Set up any headers you want here.
  w.WriteHeader(http.StatusNoContent) // send the headers with a 204 response code.
}

will send a 204 status to the client.

like image 105
Jeremy Wall Avatar answered Oct 11 '22 02:10

Jeremy Wall