Giter VIP home page Giter VIP logo

Comments (6)

asbjornu avatar asbjornu commented on April 28, 2024

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.

asbjornu avatar asbjornu commented on April 28, 2024

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.

asbjornu avatar asbjornu commented on April 28, 2024

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.

asbjornu avatar asbjornu commented on April 28, 2024

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.

jarrodhroberson avatar jarrodhroberson commented on April 28, 2024

from examples.

asbjornu avatar asbjornu commented on April 28, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.