What is the common convention for supporting multiple representation (e.g. html, json, xml) for resources (e.g. blog, user) in django?
First, I don't know how I should format my urls. For example, what your take on using either of these urls to request xml format
/<resource>.<format>
, e.g. /blogs/123.xml
/<format>/<resource>
, e.g. /xml/blogs/123
/<resource>?format=<format>
, e.g. /blogs/123?format=xml
Should I just rely on the Content-Type
passed parameter? What about having multiple mobile representation (e.g. iphone, mobile, palm) and full browser representation?
What about views? What's the convention for choosing the right templates without having a lot of if statements or much duplicate code.
Definition of a Content Type. In technical terms, a Content Type is a reusable collection of metadata for a category of content, with its corresponding taxonomies that allows you to manage information in a centralized, reusable way.
Accept header is used by HTTP clients to tell the server which type of content they expect/prefer as response. Content-type can be used both by clients and servers to identify the format of the data in their request (client) or response (server) and, therefore, help the other part interpret correctly the information.
Some common examples of content types are “text/plain”, “application/xml”, “text/html”, “application/json”, “image/gif”, and “image/jpeg”. Similarly, to determine what type of representation is desired on the client-side, an HTTP header ACCEPT is used.
What I might do, if this were to work out, is:
Accept
header (I think that's what you were talking about) and decide which content-type to send back based on the Accept
header.Accept
header.For this solution, content-types in the URL would always be represented as an associated file extension, neither part of the query-string nor part of the resource name. But aside from browser-generated requests, the content-types should be coming in through the Accept
header.
So the request comes in as:
GET /blogs/123.xml HTTP/1.1
Host: example.com
The middleware transforms that to:
GET /blogs/123 HTTP/1.1
Host: example.com
Accept: application/xml
Your view sees application/xml
and returns a response with XML content.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With