本文整理匯總了Golang中github.com/go-swagger/go-swagger/httpkit.ContentType函數的典型用法代碼示例。如果您正苦於以下問題:Golang ContentType函數的具體用法?Golang ContentType怎麽用?Golang ContentType使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ContentType函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ContentType
// ContentType gets the parsed value of a content type
func (c *Context) ContentType(request *http.Request) (string, string, *errors.ParseError) {
if v, ok := context.GetOk(request, ctxContentType); ok {
if val, ok := v.(*contentTypeValue); ok {
return val.MediaType, val.Charset, nil
}
}
mt, cs, err := httpkit.ContentType(request.Header)
if err != nil {
return "", "", err
}
context.Set(request, ctxContentType, &contentTypeValue{mt, cs})
return mt, cs, nil
}
示例2: BindValidRequest
// BindValidRequest binds a params object to a request but only when the request is valid
// if the request is not valid an error will be returned
func (c *Context) BindValidRequest(request *http.Request, route *MatchedRoute, binder RequestBinder) error {
var res []error
requestContentType := "*/*"
// check and validate content type, select consumer
if httpkit.HasBody(request) {
ct, _, err := httpkit.ContentType(request.Header)
if err != nil {
res = append(res, err)
} else {
if err := validateContentType(route.Consumes, ct); err != nil {
res = append(res, err)
}
if len(res) == 0 {
cons, ok := route.Consumers[ct]
if !ok {
res = append(res, errors.New(500, "no consumer registered for %s", ct))
} else {
route.Consumer = cons
requestContentType = ct
}
}
}
}
// check and validate the response format
if len(res) == 0 && httpkit.HasBody(request) {
if str := NegotiateContentType(request, route.Produces, requestContentType); str == "" {
res = append(res, errors.InvalidResponseFormat(request.Header.Get(httpkit.HeaderAccept), route.Produces))
}
}
// now bind the request with the provided binder
// it's assumed the binder will also validate the request and return an error if the
// request is invalid
if binder != nil && len(res) == 0 {
if err := binder.BindRequest(request, route); err != nil {
return err
}
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
示例3: BindValidRequest
// BindValidRequest binds a params object to a request but only when the request is valid
// if the request is not valid an error will be returned
func (c *Context) BindValidRequest(request *http.Request, route *MatchedRoute, binder RequestBinder) error {
var res []error
// check and validate content type, select consumer
if httpkit.CanHaveBody(request.Method) {
ct, _, err := httpkit.ContentType(request.Header)
if err != nil {
res = append(res, err)
} else {
if err := validateContentType(route.Consumes, ct); err != nil {
res = append(res, err)
}
route.Consumer = route.Consumers[ct]
}
}
// check and validate the response format
if len(res) == 0 {
if str := httputil.NegotiateContentType(request, route.Produces, ""); str == "" {
res = append(res, errors.InvalidResponseFormat(request.Header.Get(httpkit.HeaderAccept), route.Produces))
}
}
// now bind the request with the provided binder
// it's assumed the binder will also validate the request and return an error if the
// request is invalid
if binder != nil && len(res) == 0 {
if err := binder.BindRequest(request, route); err != nil {
res = append(res, err)
}
}
if len(res) > 0 {
return errors.CompositeValidationError(res...)
}
return nil
}
示例4: Bind
func (p *untypedParamBinder) Bind(request *http.Request, routeParams RouteParams, consumer httpkit.Consumer, target reflect.Value) error {
// fmt.Println("binding", p.name, "as", p.Type())
switch p.parameter.In {
case "query":
data, custom, err := p.readValue(request.URL.Query(), target)
if err != nil {
return err
}
if custom {
return nil
}
return p.bindValue(data, target)
case "header":
data, custom, err := p.readValue(request.Header, target)
if err != nil {
return err
}
if custom {
return nil
}
return p.bindValue(data, target)
case "path":
data, custom, err := p.readValue(routeParams, target)
if err != nil {
return err
}
if custom {
return nil
}
return p.bindValue(data, target)
case "formData":
var err error
var mt string
mt, _, e := httpkit.ContentType(request.Header)
if e != nil {
// because of the interface conversion go thinks the error is not nil
// so we first check for nil and then set the err var if it's not nil
err = e
}
if err != nil {
return errors.InvalidContentType("", []string{"multipart/form-data", "application/x-www-form-urlencoded"})
}
if mt != "multipart/form-data" && mt != "application/x-www-form-urlencoded" {
return errors.InvalidContentType(mt, []string{"multipart/form-data", "application/x-www-form-urlencoded"})
}
if mt == "multipart/form-data" {
if err := request.ParseMultipartForm(defaultMaxMemory); err != nil {
return errors.NewParseError(p.Name, p.parameter.In, "", err)
}
}
if err := request.ParseForm(); err != nil {
return errors.NewParseError(p.Name, p.parameter.In, "", err)
}
if p.parameter.Type == "file" {
file, header, err := request.FormFile(p.parameter.Name)
if err != nil {
return errors.NewParseError(p.Name, p.parameter.In, "", err)
}
target.Set(reflect.ValueOf(httpkit.File{Data: file, Header: header}))
return nil
}
if request.MultipartForm != nil {
data, custom, err := p.readValue(url.Values(request.MultipartForm.Value), target)
if err != nil {
return err
}
if custom {
return nil
}
return p.bindValue(data, target)
}
data, custom, err := p.readValue(url.Values(request.PostForm), target)
if err != nil {
return err
}
if custom {
return nil
}
return p.bindValue(data, target)
case "body":
newValue := reflect.New(target.Type())
if err := consumer.Consume(request.Body, newValue.Interface()); err != nil {
if err == io.EOF && p.parameter.Default != nil {
target.Set(reflect.ValueOf(p.parameter.Default))
return nil
}
tpe := p.parameter.Type
if p.parameter.Format != "" {
//.........這裏部分代碼省略.........