Standard library / net/http/httptest

httptest.ResponseRecorder.WriteHeader()

Records the first response status; subsequent calls do not replace it. Expected stdout: '201\n'.

Formal Syntax & Structure

go syntaxgo
func (rw *ResponseRecorder) WriteHeader(code int)

Parameters & Attributes

NameTypeDescription
codeintHTTP status code.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http/httptest")
func main() {  w := httptest.NewRecorder()  w.WriteHeader(201)  w.WriteHeader(400)  fmt.Println(w.Result().StatusCode)}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference