gin.Negotiate Example Code

It would be very helpful if someone could post an example that explains how to return different format responses based on the Request Accept header.

I have searched for a long time and can not find any examples of how to actually render a response based on the Accept header.
And definitely not how to render based on a custom MediaType that has an API version number in it (“application/;version=1.0.0”) or (“application/”, )

Here is what I have figured out so far:

func Health(c *gin.Context) {
	startupTime := c.MustGet("startupTime").(time.Time)
	status := models.NewHealth(startupTime)
	c.Negotiate(http.StatusOK, gin.Negotiate{
		Offered:  []string{"application/;version=1.0.0", gin.MIMEJSON, gin.MIMEYAML, gin.MIMEXML, gin.MIMEHTML},
		HTMLName: "",
		HTMLData: status,
		JSONData: status,
		XMLData:  status,
		YAMLData: status,
		Data:     status,

I can get back all the formats but HTML and my custom versioned MediaType which is the preferred one.

I can not find any example of what should go in HTMLName or how to register a Render for my custom MediaType.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.