Skip to content

Content negotiate custom mediatype #106

@asbjornu

Description

@asbjornu

As with #41, I'm trying to have Gin negotiate a custom mediatype. From the client, I'm sending the following request header:

Accept: application/problem+json, application/json

And on the server, I'm trying to negotiate all errors (with middleware) as such:

problem := Problem{
    Detail: errorText,
    Status: status,
    Title:  http.StatusText(status),
}
c.Negotiate(status, gin.Negotiate{
    Offered:  []string{"application/problem+json", gin.MIMEJSON, gin.MIMEHTML},
    HTMLName: "error",
    HTMLData: &problem,
    JSONData: &problem,
})

However, somewhere before I'm able to handle the error, Gin intercepts and for some reason decides that the requested Accept header can't be satisfied, writes the following to the log, and responds with 406 Not Acceptable:

Error #01: the accepted formats are not offered by the server

I would love to see a full example of how content negotiation a custom mediatype in Gin works. Would you be able to contribute your working code @jarrodhroberson?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions