Skip to content

Examples have a problem with WriteHeader()/Write() ordering #577

@ptman

Description

@ptman

Description

encoder := json.NewEncoder(rw)
if err = encoder.Encode(creation); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.Header().Set("Content-Type", "application/json; charset=utf-8")
rw.WriteHeader(http.StatusOK)

https://pkg.go.dev/net/http#ResponseWriter

WriteHeader() should be called before Write(), not after. Cannot write headers once the body has been written. In the other examples as well.

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions