Releasing a Go library for content-type negotiation

Featured image for sharing metadata for article

As I've written about in the past, I'm a big fan of using Server-Driven Content Negotiation for APIs for versioning, but also find it handy for being able to serve different representations of data depending on what the client supports. This is handy to be able to serve an HTML page to a browser doing a form-post, compared to JSON to an API client doing the same call.

As I'm starting to work on a service that would do this in Go, I realised that I would need to be able to correctly perform content negotiation. Fortunately, I've already done this for Java so I thought I'd make a stab at building my own.

Just before I did, I thought I'd check to see if there's any prior art, and discovered this feature request to Go to add content negotation, and found a couple of implementations, but due to their license, found that it wouldn't be widely usable, so alas, I ended up writing gitlab.com/jamietanna/content-negotiation-go.

I'd seen that the Go standard library had support for parsing media types which is probably the hardest part, but it turns out it didn't do quite enough for me, so I ended up needing to write some convenience methods for some of the other requirements.

While doing this, I realised that my Java library was actually missing some key functionality with how it negotiates, so I've ended up releasing a fix for that, too. Because of how difficult it can be, especially around finding the right combinations to ensure you've thought of all the options, I'll be releasing a set of test cases, likely in JSON format, that can be used to ensure that your implementations are correct.

My library should now cover all the primary use cases for content negotiation, and I'll be working to get it ready for a v1 release, as well as seeing if some of it can actually go upstream into the language!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#go #content-negotiation.

Also on:

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.