9c61295efe
* fix(binding): support application/yaml RFC 9512 defines application/yaml as the official YAML MIME type. application/x-yaml is deprecated. In this commit, we ensure it is recognized correctly in Content-Type. * fix(render): use application/yaml when rendering YAML As per RFC 9512, application/x-yaml is now deprecated and applications should use application/yaml. This commit fix the Content-Type header when rendering YAML.
37 lines
813 B
Go
37 lines
813 B
Go
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package render
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// YAML contains the given interface object.
|
|
type YAML struct {
|
|
Data any
|
|
}
|
|
|
|
var yamlContentType = []string{"application/yaml; charset=utf-8"}
|
|
|
|
// Render (YAML) marshals the given interface object and writes data with custom ContentType.
|
|
func (r YAML) Render(w http.ResponseWriter) error {
|
|
r.WriteContentType(w)
|
|
|
|
bytes, err := yaml.Marshal(r.Data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = w.Write(bytes)
|
|
return err
|
|
}
|
|
|
|
// WriteContentType (YAML) writes YAML ContentType for response.
|
|
func (r YAML) WriteContentType(w http.ResponseWriter) {
|
|
writeContentType(w, yamlContentType)
|
|
}
|