Comments (6)
It's weird, because if I do c.NegotiateFormat("application/problem+json", gin.MIMEJSON, gin.MIMEHTML)
, I get application/problem+json
in return as expected. However, c.Negotiate()
somehow comes to another conclusion and responds with 406 Not Acceptable
.
from examples.
Ok, after digging into the Gin source code, I think I understand what's going on. In context.go:1110-1135
, Negotiate()
only supports MIMEJSON
, MIMEHTML
, MIMEXML
, MIMEYAML
and MIMETOML
:
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 +json
and +xml
was translated into MIMEJSON
and MIMEXL
respectively, but knowing this, I should be able to circumvent it somehow.
from examples.
Hm, no. I seem unable to properly set the Content-Type
of the response to … anything, really. For some weird reason Gin responds with:
Content-Type: text/plain; charset=utf-8
Even though I've explicitly set c.Header("Content-Type", "application/problem+json")
. This is my handler code now:
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 c.Header("Content-Type", negotiatedMimeType)
work here?
from examples.
As you closed #41, were you able to set the Content-Type
of the response @jarrodhroberson? If so, could you please post a full example of how you got it to work?
from examples.
from examples.
@jarrodhroberson, so with the following, you're able to have Gin respond with Content-Type: application/vnd.health.json;version=1.0.0
?
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 c.Negotiate()
only supports MIMEJSON
, MIMEHTML
, MIMEXML
, MIMEYAML
and MIMETOML
. Any other MIME type and it will do c.AbortWithError()
.
from examples.
Related Issues (20)
- file-upload requires fragment upload and breakpoint continuation
- context.Next()
- support for websocket middleware
- How can i add or remove router after gin server started dynamically? HOT 1
- How can i add or remove middleware after gin server started dynamically? HOT 1
- How can I install Gin framework? HOT 2
- Update the gin package to resolve GO-2023-1737 HOT 1
- Disregard - found the issue HOT 1
- Graceful shutdown example outdated
- Add json example HOT 1
- How about an example with GO-CAS ? HOT 1
- How can i generate assets.go??? HOT 2
- SSEvent example small changes to make it work HOT 3
- body is null in LoggerWithFormatter
- Does gin support global context for html render?
- How to Support WebSocket HOT 2
- The following error exists in the http2 package HOT 2
- file is exist!! HOT 1
- error: examples/http2/main.go
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from examples.