forked from nokia/restful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contenttype.go
45 lines (39 loc) · 1.47 KB
/
contenttype.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2021 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package restful
import (
"net/http"
"strings"
)
// ContentType strings
const (
AcceptHeader = "Accept"
ContentTypeHeader = "Content-type"
ContentTypeAny = "*/*"
ContentTypeApplicationAny = "application/*"
ContentTypeApplicationJSON = "application/json"
ContentTypeProblemJSON = "application/problem+json"
ContentTypePatchJSON = "application/json-patch+json" // RFC 6902
ContentTypeMergeJSON = "application/merge-patch+json" // RFC 7386
ContentTypeForm = "application/x-www-form-urlencoded"
ContentTypeMultipartForm = "multipart/form-data"
)
// BaseContentType returns the MIME type of the Content-Type header as lower-case string
// E.g.: "Content-Type: application/JSON; charset=ISO-8859-1" --> "application/json"
func BaseContentType(contentType string) string {
ct := strings.TrimSpace(contentType)
ct = strings.ToLower(ct)
return strings.Split(ct, ";")[0]
}
// GetBaseContentType returns base content type from HTTP header.
// E.g.: "Content-Type: application/JSON; charset=ISO-8859-1" --> "application/json"
func GetBaseContentType(headers http.Header) string {
return BaseContentType(headers.Get(ContentTypeHeader))
}
func isJSONContentType(ct string) bool {
return ct == ContentTypeApplicationJSON ||
ct == ContentTypePatchJSON ||
ct == ContentTypeMergeJSON ||
ct == ContentTypeProblemJSON
}