MCPcopy Create free account
hub / github.com/goadesign/goa / SetContentType

Function SetContentType

http/encoding.go:284–304  ·  view source on GitHub ↗

SetContentType initializes the response Content-Type header given a MIME type. If the Content-Type header is already set and the MIME type is "application/json" or "application/xml" then SetContentType appends a suffix to the header ("+json" or "+xml" respectively).

(w http.ResponseWriter, ct string)

Source from the content-addressed store, hash-verified

282// "application/json" or "application/xml" then SetContentType appends a suffix
283// to the header ("+json" or "+xml" respectively).
284func SetContentType(w http.ResponseWriter, ct string) {
285 h := w.Header().Get("Content-Type")
286 if h == "" {
287 w.Header().Set("Content-Type", ct)
288 return
289 }
290 // RFC6839 only defines suffixes for a few mime types, we only concern
291 // ourselves with JSON and XML.
292 if ct != "application/json" && ct != "application/xml" {
293 w.Header().Set("Content-Type", ct)
294 return
295 }
296 if strings.Contains(h, "+") {
297 return
298 }
299 suffix := "+json"
300 if ct == "application/xml" {
301 suffix = "+xml"
302 }
303 w.Header().Set("Content-Type", h+suffix)
304}
305
306func newTextEncoder(w io.Writer, ct string) Encoder {
307 return &textEncoder{w, ct}

Callers 1

ResponseEncoderFunction · 0.85

Calls 3

SetMethod · 0.80
GetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected