Skip to main content

📖 String

String writes the data parameter with the given status code to the http.ResponseWriter The Content-Type header is set accordingly to text/plain

func ExampleStringGet(request there.Request) there.Response {
return there.String(status.OK, "Hello there")
}

When this handler gets called, the final rendered result will be

Hello there
func String(code int, data string) Response {
return stringResponse{code: code, data: []byte(data)}
}

type stringResponse struct {
code int
data []byte
}

func (s stringResponse) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(s.code)
rw.Header().Set(header.ContentType, ContentTypeTextPlain)
_, err := rw.Write(s.data)
if err != nil {
log.Printf("stringResponse: ServeHttp write failed: %v", err)
}
}