Skip to content

获取 Content-Type 的小问题 #16

Description

@xmx

ship/context.go

Lines 410 to 417 in 405a71d

// ContentType returns the Content-Type of the request without the charset.
func (c *Context) ContentType() (ct string) {
ct = c.req.Header.Get(HeaderContentType)
if index := strings.IndexAny(ct, ";"); index > 0 {
ct = strings.TrimSpace(ct[:index])
}
return
}

ship/binder.go

Lines 72 to 83 in 405a71d

// Bind implements the interface Binder, which looks up the registered binder
// by the request header "Content-Type" and calls it to bind the value dst
// to req.
func (mb *MuxBinder) Bind(dst interface{}, req *http.Request) error {
ct := req.Header.Get("Content-Type")
if index := strings.IndexAny(ct, ";"); index > 0 {
ct = strings.TrimSpace(ct[:index])
}
if ct == "" {
return ErrMissingContentType
}

上面两处代码在获取 Content-Type 时用的是 index > 0,如果测试用例是 ; charset=utf-8,那么切割出的 Content-Type; charset=utf-8。此时我们期望结果的应该是 空字符串

故:此处使用 index >= 0 更为妥当

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions