[Dancer-users] one route, different actions based on context

Puneet Kishor punk.kish at gmail.com
Sat Dec 25 00:02:51 CET 2010



Alastair Sherringham wrote:
> On 24 December 2010 19:48, Puneet Kishor<punk.kish at gmail.com>  wrote:
>>> $ curl http://application/foo/all
>>>
>>> a json stream of all the foo widgets is returned.
>
> There's an advent calendar up just now with some advice on this sort
> of thing e.g.
>
> Writing REST web services with Dancer
> http://advent.perldancer.org/2010/8
>
> Some curl usage - maybe use "application/json"? e.g.
>
> curl -H 'Accept-Type: application/json'<url>


Thanks. A great article, helps clear the fog further.

Nevertheless, my problem now is as I articulated in my last email. What 
I want is to send back the template only if being request via a browser 
as a regular, non-Ajax request  (how should I determine that?). 
Otherwise, if it is being called via Ajax or from the command line via 
curl or lwp or whatever, a json stream should be returned. So, given 
(pseudocode ahead) --

  1 prepare_serializer_for_format;
  2
  3 get '/all.:format' => sub {
  4
  5 	my $res = query->db->for->all;
  6 	
  7 	# Requested a full web page from the browser, so
  8 	# use the template; no need to use a serializer
  9 	if (request->came_from_browser_non_ajax) {
10 		template 'all.tt', {res => $res, other => $options};
11 	}
12 	
13 	# For ajax requests or requests from command line,
14 	# use the requested serializer and return a text
15 	# stream
16 	else {
17 		return to_json $res;
18 	}
19
20 };

The idea is that a user can go to http://server/foo/all and see the 
entire web page as rendered by the template 'all.tt', or the user can 
request a specific serialized format via the command line and get a text 
stream back. Would be nice to have a default serialization format 
defined, so, for example, if no specific format is requested then JSON 
can be sent back, else, XML or whatever can be sent back.



>
> Cheers,
>
>



-- 
Puneet Kishor


More information about the Dancer-users mailing list