You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does Bottle support content negotiation? Sometimes it's the client that specifies to the server what format it wants back, for example the same URL can be used to return an HTML version of the resource, or JSON, or RDF, or XML. Can Bottle support this? More or less like this:
@route('/resource', accept-header='text/html'):
def html():
return resource as html
@route('/resource', accept-header='text/turtle'):
def turtle():
return resource as turtle file
@route('/resource', accept-header='application/xml'):
def xml():
return resource as xml file
The text was updated successfully, but these errors were encountered:
No. Content negotiation is quite complex (if you actually want to implement the spec) and also a completely optional part of HTTP. Perhaps caused by the complexity and unintuitive rules, it is not used very widely. Most REST APIs or traditional webpages only offer a single representation per route and completely ignore the Accept header. Some even side-step the spec and allow users to add common file name extensions to request routes to choose an output format.
If you really need that, I'd suggest implementing it as a plugin or simple helper function hat calls different serializers and/or handler functions based on the best match. For example:
I've just noticed that pyramid has something like this. See here, scroll down to "Predicate Arguments > header". Basically, before a request is dispatched to a controller it's possible to check if a header exist in addition to the method and pattern of the request. Would this be possible to do for Bottle as well? It wouldn't seem too difficult to check for a header, but I don't know...
Does Bottle support content negotiation? Sometimes it's the client that specifies to the server what format it wants back, for example the same URL can be used to return an HTML version of the resource, or JSON, or RDF, or XML. Can Bottle support this? More or less like this:
The text was updated successfully, but these errors were encountered: