[Dancer-users] Where do I get POST form data in a post route?

Leandro Hermida softdev at leandrohermida.com
Sat Jun 25 20:52:42 CEST 2011


Hi,

Just want to say thanks for the email replies to help, it was actually
really straightforward to take existing CGI.pm scripts and migrate them to
the Dancer framework, for the first major step simply copying the CGI code
as is into Dancer routes and changing obvious things like:

- print $html --> return $html
- $cgi->params --> params
- print $cgi->redirect(...) --> redirect '...'
- print $cgi->header(...) --> headers '...'

After these small changes everything works just like it did before and you
can use the web app as is.  As a next step I have to start using the
templating and layout functionality and all the other nice features.

thanks again,
Leandro

On Fri, Jun 24, 2011 at 5:02 PM, Leandro Hermida <softdev at leandrohermida.com
> wrote:

> Hi again,
>
> I've done what I wrote above with headers just before returning the data
> file string and it works perfectly :)
>
> thanks,
> Leandro
>
>
> On Fri, Jun 24, 2011 at 4:30 PM, Maurice Mengel <mauricemengel at gmail.com>wrote:
>
>> there is are two related Dancer keywords: header and a headers
>>
>> http://search.cpan.org/~sukria/Dancer-1.3060/lib/Dancer.pm#headers
>>
>> sorry no time to answer properly.
>>
>> have never used it myself.
>>
>>
>> On Fri, Jun 24, 2011 at 4:06 PM, Leandro Hermida
>> <softdev at leandrohermida.com> wrote:
>> > Crap hit send before finished
>> >
>> > On Fri, Jun 24, 2011 at 4:05 PM, Leandro Hermida
>> > <softdev at leandrohermida.com> wrote:
>> >>
>> >> Hi thank you for the feeback,
>> >>
>> >> What I would like to do as I have in my old CGI.pm scripts would be
>> >> translated as following in Dancer:
>> >>
>> >> get '/data/:id' => sub {
>> >>     # preexisting code here to get $file_data into variable from DB or
>> >> whereever for a specified :id (already have this as is no need to
>> change)
>> >>     # --> want to set right here before returning $file_data the right
>> >> headers using some Dancer commands, particularly how do I set
>> >> content_disposition?
>> >>     # --> want to also make sure if Dancer set some headers by default
>> >> that I don't want that they are removed
>> >>     return $file_data;
>> >> };
>> >>
>> >> How would one using Dancer do the --> parts? Is it literally:
>> >>
>> >> headers 'Conten-type ' => 'text/plain', X-Bar => 'foo';
>> >>
>> >>
>> >
>> > headers 'Content-type' => 'text/plain', 'Encoding' => 'utf-8',
>> > 'Content-disposition' => 'inline; filename=myfile.txt';
>> >
>> >
>> >>
>> >> best,
>> >> Leandro
>> >>
>> >> On Fri, Jun 24, 2011 at 3:52 PM, Maurice Mengel <
>> mauricemengel at gmail.com>
>> >> wrote:
>> >>>
>> >>> I have an dancer application that outputs xml. I set mime default in
>> >>> config.yml to txt/html to serve static pages and use
>> >>> mime to set the mime type, see
>> >>> http://search.cpan.org/~sukria/Dancer-1.3060/lib/Dancer.pm#mime for
>> >>> that route.
>> >>>
>> >>> I return data in utf-8 via dancer as normal. UTF-8 should work now in
>> >>> Dancer.
>> >>>
>> >>> My guess is that you can use Dancer::FileUtils if you to load files
>> >>> and munge them before serving. If you don't want munge them you should
>> >>> be able to serve them directly as static files from the public
>> >>> directory.
>> >>>
>> >>>
>> >>>
>> >>> On Fri, Jun 24, 2011 at 3:44 PM, Leandro Hermida
>> >>> <softdev at leandrohermida.com> wrote:
>> >>> > Hi everyone,
>> >>> >
>> >>> > Thanks for all the help, my bad :) I had quickly read the Dancer
>> Intro
>> >>> > and
>> >>> > Tutorial but not the Cookbook as I thought that would come later.
>> It's
>> >>> > actually very quick to move the CGI.pm scripts to Dancer I already
>> have
>> >>> > all
>> >>> > the GETs working for HTML and now the form POSTs.
>> >>> >
>> >>> > Another question, I had a couple CGI scripts which would return
>> >>> > non-HTML,
>> >>> > e.g. like retrieve a data file... this would require printing in CGI
>> a
>> >>> > specific header before the content body e.g.
>> >>> >
>> >>> > $cgi->header(-type                => 'text/plain',
>> >>> >                    -charset             => 'utf-8',
>> >>> >                    -encoding            => 'utf-8',
>> >>> >                    -content_disposition => "inline;
>> >>> > filename=$filename"),
>> >>> >
>> >>> > How do I set such headers in Dancer, removing any default headers
>> >>> > Dancer has
>> >>> > already has set?  I looked at header and headers but don't
>> completely
>> >>> > understand whether it removes all the preexisting headers Dancer had
>> >>> > set for
>> >>> > me.
>> >>> >
>> >>> > best,
>> >>> > Leandro
>> >>> >
>> >>> > On Fri, Jun 24, 2011 at 3:32 PM, Maurice Mengel
>> >>> > <mauricemengel at gmail.com>
>> >>> > wrote:
>> >>> >>
>> >>> >> Dancer::Cookbook describes like this. (Maybe we should add a code
>> >>> >> example
>> >>> >> there)
>> >>> >>
>> >>> >> > Retrieving request parameters
>> >>> >> > The params method returns a hashref of request parameters; these
>> >>> >> > will be
>> >>> >> > parameters supplied on the query string, within the path itself
>> >>> >> > (with named
>> >>> >> > placeholders), and, for HTTTP POST requests, the content of the
>> POST
>> >>> >> > body.
>> >>> >>
>> >>> >> For examples:
>> >>> >>
>> >>> >> http://advent.perldancer.org/2010
>> >>> >>
>> >>> >> e.g. the third 3 example.
>> >>> >>
>> >>> >> which has the example for accessing post variables via
>> >>> >>   params->{'title'},
>> >>> >>   params->{'text'}
>> >>> >>
>> >>> >> @ambs:
>> >>> >>
>> >>> >>  $hash = params;
>> >>> >>
>> >>> >> might better be written as
>> >>> >>  $hashref = params;
>> >>> >> or even
>> >>> >>  $hashref = params();
>> >>> >>
>> >>> >> but probabily it doesn't matter that much ;-)
>> >>> >>
>> >>> >> maurice
>> >>> >>
>> >>> >>
>> >>> >> On Fri, Jun 24, 2011 at 3:22 PM, Leandro Hermida
>> >>> >> <softdev at leandrohermida.com> wrote:
>> >>> >> > Hi again,
>> >>> >> >
>> >>> >> > Sorry another newbie question, in a post route where do I get the
>> >>> >> > POST
>> >>> >> > form
>> >>> >> > data?  A la the CGI.pm $cgi->Vars hash
>> >>> >> >
>> >>> >> > thanks again,
>> >>> >> > Leandro
>> >>> >> >
>> >>> >> >
>> >>> >> > _______________________________________________
>> >>> >> > Dancer-users mailing list
>> >>> >> > Dancer-users at perldancer.org
>> >>> >> > http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
>> >>> >> >
>> >>> >> >
>> >>> >
>> >>> >
>> >>
>> >
>> >
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20110625/9d56dfcd/attachment.htm>


More information about the Dancer-users mailing list