mime.ParseMediaType and mime.FormatMediaType
func ParseMediaType(v string) (mediatype string, params map[string]string, err error)
func FormatMediaType(t string, param map[string]string) stringParse a type with charset and a quoted parameter, format a type, and show an invalid parameter error.
| Parameter | Description |
|---|---|
v | A media type string, including optional parameters, such as a Content-Type header value. |
t, param | The type and parameter map given to FormatMediaType. Names are written in lowercase. |
Returns: ParseMediaType returns the media type, a parameter map, and an error. FormatMediaType returns the serialized string, or empty on a standard violation.
Errors
ErrInvalidMediaParameter— Returned when the type is present but a parameter cannot be parsed, as in text/plain; charset without a value. The example prints the error string.
- The quoted parameter foo="Bar" is returned with the value Bar, case preserved, under the lowercase key foo.
package main
import (
"fmt"
"mime"
)
func main() {
media, params, err := mime.ParseMediaType(`text/plain; charset=utf-8; foo="Bar"`)
fmt.Println(media, params["charset"], params["foo"], err)
fmt.Println(mime.FormatMediaType("text/plain", map[string]string{"charset": "utf-8"}))
_, _, err = mime.ParseMediaType("text/plain; charset")
fmt.Println(err)
}Expected output
text/plain utf-8 Bar <nil>
text/plain; charset=utf-8
mime: invalid media parameterRelated: mime/multipart