Rendering and content negotiation
Web handlers can return a variety of content types, for example, they can return JSON, plain text, images, and more. Frequently, when communicating with APIs, it's possible to specify and accept content type to clarify what format you'll pass data in as and what data you want to receive back out.
This recipe will explore using unrolled/render and a custom function to negotiate content type and respond accordingly.
Getting ready
Configure your environment according to these steps:
- Refer to the steps in the Getting ready section of the Working with web handlers, requests, and ResponseWriters recipe.
- Run the
go get github.com/unrolled/render
command.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create and navigate to the
chapter7/negotiate
directory. - Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter7/negotiate or use this as an exercise to write some of your own code.
- Create...