• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang httputil.NegotiateContentType函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/golang/gddo/httputil.NegotiateContentType函数的典型用法代码示例。如果您正苦于以下问题:Golang NegotiateContentType函数的具体用法?Golang NegotiateContentType怎么用?Golang NegotiateContentType使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NegotiateContentType函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: contentType

func contentType(req *http.Request) int {
	str := httputil.NegotiateContentType(req, []string{"text/plain", "application/json"}, "text/plain")
	if str == "application/json" {
		return ContentJSON
	} else {
		return ContentText
	}
}
开发者ID:alena1108,项目名称:rancher-metadata,代码行数:8,代码来源:main.go


示例2: TestNegotiateContentType

func TestNegotiateContentType(t *testing.T) {
	for _, tt := range negotiateContentTypeTests {
		r := &http.Request{Header: http.Header{"Accept": {tt.s}}}
		actual := httputil.NegotiateContentType(r, tt.offers, tt.defaultOffer)
		if actual != tt.expect {
			t.Errorf("NegotiateContentType(%q, %#v, %q)=%q, want %q", tt.s, tt.offers, tt.defaultOffer, actual, tt.expect)
		}
	}
}
开发者ID:maddyonline,项目名称:gddo,代码行数:9,代码来源:negotiate_test.go


示例3: ResponseFormat

// ResponseFormat negotiates the response content type
func (c *Context) ResponseFormat(r *http.Request, offers []string) string {
	if v, ok := context.GetOk(r, ctxResponseFormat); ok {
		if val, ok := v.(string); ok {
			return val
		}
	}

	format := httputil.NegotiateContentType(r, offers, "")
	if format != "" {
		context.Set(r, ctxResponseFormat, format)
	}
	return format
}
开发者ID:jason-xxl,项目名称:go-swagger,代码行数:14,代码来源:context.go


示例4: EncodeResponse

// EncodeResponse uses registered Encoders to marshal the response body based on the request
// `Accept` header and writes it to the http.ResponseWriter
func (app *Application) EncodeResponse(ctx *Context, v interface{}) error {
	contentType := httputil.NegotiateContentType(ctx.Request(), app.encodableContentTypes, "*/*")
	p := app.encoderPools[contentType]

	// the encoderPool will handle whether or not a pool is actually in use
	encoder := p.Get(ctx)
	if err := encoder.Encode(v); err != nil {
		// TODO: log out error details
		return err
	}
	p.Put(encoder)

	return nil
}
开发者ID:minloved,项目名称:goa,代码行数:16,代码来源:encoding.go


示例5: TypeNegotiator

// TypeNegotiator returns a content type negotiation handler.
//
// The method takes a list of response MIME types that are supported by the application.
// The negotiator will determine the best response MIME type to use by checking the "Accept" HTTP header.
// If no match is found, the first MIME type will be used.
//
// The negotiator will set the "Content-Type" response header as the chosen MIME type. It will call routing.Context.SetDataWriter()
// to set the appropriate data writer that can write data in the negotiated format.
//
// If you do not specify any supported MIME types, the negotiator will use "text/html" as the response MIME type.
func TypeNegotiator(formats ...string) routing.Handler {
	if len(formats) == 0 {
		formats = []string{HTML}
	}
	for _, format := range formats {
		if _, ok := DataWriters[format]; !ok {
			panic(format + " is not supported")
		}
	}

	return func(c *routing.Context) error {
		format := httputil.NegotiateContentType(c.Request, formats, formats[0])
		DataWriters[format].SetHeader(c.Response)
		c.SetDataWriter(DataWriters[format])
		return nil
	}
}
开发者ID:go-ozzo,项目名称:ozzo-routing,代码行数:27,代码来源:type.go


示例6: contentType

func contentType(req *http.Request) int {
	str := httputil.NegotiateContentType(req, []string{
		"text/plain",
		"application/json",
		"application/yaml",
		"application/x-yaml",
		"text/yaml",
		"text/x-yaml",
	}, "text/plain")

	if strings.Contains(str, "json") {
		return ContentJSON
	} else if strings.Contains(str, "yaml") {
		return ContentYAML
	} else {
		return ContentText
	}
}
开发者ID:rancher,项目名称:rancher-metadata,代码行数:18,代码来源:main.go


示例7: selectContentMarshaler

func selectContentMarshaler(r *http.Request, marshalers map[string]ContentMarshaler) (marshaler ContentMarshaler, contentType string) {
	if _, found := r.Header["Accept"]; found {
		var contentTypes []string
		for ct := range marshalers {
			contentTypes = append(contentTypes, ct)
		}

		contentType = httputil.NegotiateContentType(r, contentTypes, defaultContentTypHeader)
		marshaler = marshalers[contentType]
	} else if contentTypes, found := r.Header["Content-Type"]; found {
		contentType = contentTypes[0]
		marshaler = marshalers[contentType]
	}

	if marshaler == nil {
		contentType = defaultContentTypHeader
		marshaler = JSONContentMarshaler{}
	}

	return
}
开发者ID:m0dd3r,项目名称:api2go,代码行数:21,代码来源:api.go


示例8: 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
}
开发者ID:jason-xxl,项目名称:go-swagger,代码行数:39,代码来源:context.go


示例9: templateExt

func templateExt(req *http.Request) string {
	if httputil.NegotiateContentType(req, []string{"text/html", "text/plain"}, "text/html") == "text/plain" {
		return ".txt"
	}
	return ".html"
}
开发者ID:sendgrid-ops,项目名称:gddo,代码行数:6,代码来源:main.go



注:本文中的github.com/golang/gddo/httputil.NegotiateContentType函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang httputil.ResponseBuffer类代码示例发布时间:2022-05-23
下一篇:
Golang gosrc.IsValidRemotePath函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap