Accept
request header and push the requested form of the content. However, as your app manages more resources and different kind of representations this can be very painful, as you may need to check for Accept-Charset
, Accept-Encoding
, put some server-side priorities , handle the errors correctly and e.t.c.Accept
header is the Negotiate
one.Negotiate
method fired, the handler MUST declare what mime types the server supports to render with safety. We can do that using the Negotiation
priorities builder.Negotitation
priorities builderNegotiation
one.Accept
struct field can be used to customize the client's Accept header manually, e.g. when the client does not contain an "application/json" mime type on its Accept
header value.NegotitationBuilder
has the necessary methods to help you prioritize mime types, charsets and encoding. Read the documentation.Build
is called automatically on Negotiate
method but it it's exported for a custom implementation of negotiation by the end-developer if ever required.Accept
header you can do that:Negotiate
method below.Negotiate
methodContext.Negotiate
method used for serving different representations of a resource at the same URI. It returns context.ErrContentNotSupported
when not matched mime types.Context.Negotiate
method accepts an interface{}
which can be any Go value or a ContentNegotiator or a ContentSelector such as the iris.N structure (see below).string(TEXT, HTML)
or []byte(Markdown, Binary)
or []byte
with any matched mime type.Context.Negotitation()
builder's content will be used instead, otherwise "v" overrides builder's content (server mime types are still retrieved by its registered, supported, mime list)iris.N
is a struct which can be passed on the Context.Negotiate
method. It contains fields which should be filled based on the Context.Negotiation()
server side values. If no matched mime then its "Other" field will be sent, which should be a string or []byte. It completes the ContentSelector
interface.interface{}
value is not a type which implements one the above then the Negotiate
method will render that based on the request's Accept
header value matching the declared priorities.v interface{}
is nil then it will uses the contents declared by the Negotiation
builder itself.