Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Type and Representations

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.

like image 834
notnoop Avatar asked Oct 14 '09 00:10

notnoop


People also ask

What Content-Type means?

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.

What is difference between Content-Type and accept?

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.

What are content types in API?

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.


1 Answers

What I might do, if this were to work out, is:

  • Your views look for the Accept header (I think that's what you were talking about) and decide which content-type to send back based on the Accept header.
  • You have a middleware which looks for an extension in the Request-URI, removes it, and adds the associated content-type to the request 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.

like image 182
yfeldblum Avatar answered Sep 30 '22 10:09

yfeldblum