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)
| 282 | // "application/json" or "application/xml" then SetContentType appends a suffix |
| 283 | // to the header ("+json" or "+xml" respectively). |
| 284 | func 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 | |
| 306 | func newTextEncoder(w io.Writer, ct string) Encoder { |
| 307 | return &textEncoder{w, ct} |
no test coverage detected