Response Formats

0 views
Skip to first unread message

Tony Nelson

unread,
May 23, 2011, 9:20:54 AM5/23/11
to ColdMVC
Currently requests support multiple formats (html, js, pdf, xml) with
the default format being html.

For example, a request to http://myapp.com/user/show/1 will output the
view as html, but requests to http://myapp.com/user/show/1.pdf or
http://myapp.com/user/show/1?format=pdf will output the view as a pdf.

For js and xml formats, your actions need to opt-in using the @formats
annotation on the action. For example, if you want your action to
support only html and xml format, you'd set "@formats html, xml" on
your controller's action.

Also, for js and xml formats, the params are automatically serialized
and output for you by default. If you want to change that, you can
create a format-specific view for outputting your response simply by
creating a new view with a format-specific extension. For example, if
you create a view located at /views/user/show.xml.cfm, it will be
automatically rendered for calls to http://myapp.com/user/show/1.xml
rather than just outputting the serialized params. This can be
important if you don't want to fully serialized your entities and
instead only return certain properties for certain requests.

When it comes to js reponses, what would people prefer the suffix to
be. Currently it's "js", so http://myapp.com/user/show/1.js will
return a JSON-serialized response and /views/user/show.js.cfm will
render a format-specific view. I think this makes the sense, but would
people prefer it to be "json" instead of "js"? I think I prefer /user/
list.js, but user/list?format=json. Currently ColdMVC checks to see if
the format is json and switches it to js instead, but does that make
the most sense? Should calls to getFormat() return "js" or "json"?

Ryan Ricard

unread,
May 23, 2011, 9:35:38 AM5/23/11
to col...@googlegroups.com
I prefer to call http://myapp.com/user/show/1.json... it feels more
natural because I am requesting a json serialized response.
http://myapp.com/user/show/1.js looks like a script file is being
requested.

Tony Nelson

unread,
May 23, 2011, 10:20:40 AM5/23/11
to ColdMVC
So would you suggest I drop support for using either "js" or "json"
and make it just "json"? It might save on some confusion.

On May 23, 8:35 am, Ryan Ricard <ryan.ric...@gmail.com> wrote:
> I prefer to callhttp://myapp.com/user/show/1.json... it feels more
> natural because I am requesting a json serialized response.http://myapp.com/user/show/1.jslooks like a script file is being
> requested.
>
> On May 23, 2011, at 8:20 AM, Tony Nelson <tonynelso...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Currently requests support multiple formats (html, js, pdf, xml) with
> > the default format being html.
>
> > For example, a request tohttp://myapp.com/user/show/1will output the
> > view as html, but requests tohttp://myapp.com/user/show/1.pdfor
> >http://myapp.com/user/show/1?format=pdfwill output the view as a pdf.
>
> > For js and xml formats, your actions need to opt-in using the @formats
> > annotation on the action. For example, if you want your action to
> > support only html and xml format, you'd set "@formats html, xml" on
> > your controller's action.
>
> > Also, for js and xml formats, the params are automatically serialized
> > and output for you by default. If you want to change that, you can
> > create a format-specific view for outputting your response simply by
> > creating a new view with a format-specific extension. For example, if
> > you create a view located at /views/user/show.xml.cfm, it will be
> > automatically rendered for calls tohttp://myapp.com/user/show/1.xml

Ryan Ricard

unread,
May 23, 2011, 10:53:48 AM5/23/11
to col...@googlegroups.com
Yes, 'json' is my preference...  'show/1.json' like 'show/1.xml', is requesting a specific data format and is more consistent when the same name as the format.  @Tony: Do you have a particular reason for wanting to go the 'show/1.js' route?

Tony Nelson

unread,
May 23, 2011, 11:04:10 AM5/23/11
to col...@googlegroups.com
I'm pretty sure other frameworks (rails) allow you to link to /user/show/1.js, although I could be wrong. I'm OK with just using JSON.

On another note, I need to update ColdMVC to add the correct response content type for each format.

-Tony

Tony Nelson

unread,
May 24, 2011, 9:37:29 AM5/24/11
to ColdMVC
The current version of ColdMVC (1.3.0) now uses the "json" format
rather than "js".

On May 23, 10:04 am, Tony Nelson <tonynelso...@gmail.com> wrote:
> I'm pretty sure other frameworks (rails) allow you to link to
> /user/show/1.js, although I could be wrong. I'm OK with just using JSON.
>
> On another note, I need to update ColdMVC to add the correct response
> content type for each format.
>
> -Tony
>
>
>
>
>
>
>
> On Mon, May 23, 2011 at 9:53 AM, Ryan Ricard <ryan.ric...@gmail.com> wrote:
> > Yes, 'json' is my preference...  'show/1.json' like 'show/1.xml', is
> > requesting a specific data format and is more consistent when the same name
> > as the format.  @Tony: Do you have a particular reason for wanting to go the
> > 'show/1.js' route?
>
> > On Mon, May 23, 2011 at 9:20 AM, Tony Nelson <tonynelso...@gmail.com>wrote:
>
> >> So would you suggest I drop support for using either "js" or "json"
> >> and make it just "json"? It might save on some confusion.
>
> >> On May 23, 8:35 am, Ryan Ricard <ryan.ric...@gmail.com> wrote:
> >> > I prefer to callhttp://myapp.com/user/show/1.json... it feels more
> >> > natural because I am requesting a json serialized response.
> >>http://myapp.com/user/show/1.jslookslike a script file is being
> >> > requested.
>
> >> > On May 23, 2011, at 8:20 AM, Tony Nelson <tonynelso...@gmail.com>
> >> wrote:
>
> >> > > Currently requests support multiple formats (html, js, pdf, xml) with
> >> > > the default format being html.
>
> >> > > For example, a request tohttp://myapp.com/user/show/1willoutput the
> >> > > view as html, but requests tohttp://myapp.com/user/show/1.pdfor
> >> > >http://myapp.com/user/show/1?format=pdfwilloutput the view as a pdf.
Reply all
Reply to author
Forward
0 new messages