-
Notifications
You must be signed in to change notification settings - Fork 663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content negotiate custom mediatype #106
Comments
It's weird, because if I do |
Ok, after digging into the Gin source code, I think I understand what's going on. In func (c *Context) Negotiate(code int, config Negotiate) {
switch c.NegotiateFormat(config.Offered...) {
case binding.MIMEJSON:
data := chooseData(config.JSONData, config.Data)
c.JSON(code, data)
case binding.MIMEHTML:
data := chooseData(config.HTMLData, config.Data)
c.HTML(code, config.HTMLName, data)
case binding.MIMEXML:
data := chooseData(config.XMLData, config.Data)
c.XML(code, data)
case binding.MIMEYAML:
data := chooseData(config.YAMLData, config.Data)
c.YAML(code, data)
case binding.MIMETOML:
data := chooseData(config.TOMLData, config.Data)
c.TOML(code, data)
default:
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) //nolint: errcheck
}
} Which of course makes some sense. It would be nice if |
Hm, no. I seem unable to properly set the Content-Type: text/plain; charset=utf-8 Even though I've explicitly set problem := Problem{
Detail: errorText,
Status: status,
Title: http.StatusText(status),
}
allMimeTypes := []string{"application/problem+json", gin.MIMEJSON, gin.MIMEHTML)
negotiatedMimeType := c.NegotiateFormat(allMimeTypes...)
switch negotiatedMimeType {
case gin.MIMEHTML:
c.HTML(status, "error", &problem)
default:
c.JSON(status, &problem)
}
c.Header("Content-Type", negotiatedMimeType)
c.Abort() Why doesn't |
As you closed #41, were you able to set the |
my solution is in #41 and the example I posted works when combined with the
solution I provided when I closed that issue.
…On Fri, May 5, 2023 at 5:14 AM Asbjørn Ulsberg ***@***.***> wrote:
As you closed #41 <#41>, were
you able to set the Content-Type of the response @jarrodhroberson
<https://github.com/jarrodhroberson>? If so, could you please post a full
example of how you got it to work?
—
Reply to this email directly, view it on GitHub
<#106 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABF773MRIAP5IBNC6KZQELXETAF5ANCNFSM6AAAAAAXSDPBRU>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Jarrod Roberson
678.551.2852
|
@jarrodhroberson, so with the following, you're able to have Gin respond with func Health(c *gin.Context) {
startupTime := c.MustGet("startupTime").(time.Time)
status := models.NewHealth(startupTime)
c.Negotiate(http.StatusOK, gin.Negotiate{
Offered: []string{"application/vnd.health.json;version=1.0.0", gin.MIMEJSON, gin.MIMEYAML, gin.MIMEXML, gin.MIMEHTML},
HTMLName: "",
HTMLData: status,
JSONData: status,
XMLData: status,
YAMLData: status,
Data: status,
})
} If you read #106 (comment), I can't see how that actually works, because |
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:
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 with406 Not Acceptable
: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?
The text was updated successfully, but these errors were encountered: