How to upload files on server using Mojo::UserAgent::Transactor?

415 views
Skip to first unread message

Pavel Serikov

unread,
Apr 21, 2015, 2:41:48 AM4/21/15
to mojol...@googlegroups.com
Greetings,

I need to upload files on server using Mojo

Can anyone explain me how to do it on simple example below?

I run with morbo on http://127.0.0.1:3000 this app:

#!/usr/bin/env perl
use Mojolicious::Lite;

post
'/' => sub {
 
my $self = shift;
 
my $upload = $self->param('model');
 
my $name = $upload->filename;
  warn $name
;
  $self
->render(text => 'OK');
};

app
->start;

And run this file upload script (called it test.pl):

use Mojo::UserAgent::Transactor;
use Data::Dumper;

my $t = Mojo::UserAgent::Transactor->new;
my $tx = $t->tx(POST => 'http://127.0.0.1:3000' => form => {model => {file => 'stick.stl'}});
my $result = $tx->res->to_string;
warn
Dumper $result;

on output of ./test.pl I have:
 
$VAR1 = 'HTTP/1.1 404 Not Found
Content-Length: 0
Date: Tue, 21 Apr 2015 05:37:21 GMT
'
;

And no one warn output in server log, only regular

[Tue Apr 21 09:33:24 2015] [info] Listening at "http://*:3000".
Server available at http://127.0.0.1:3000.

What I did wrong?



Dan Book

unread,
Apr 21, 2015, 8:12:37 AM4/21/15
to mojol...@googlegroups.com
This won't actually run the transaction, you need to start it with a UserAgent object:
my $ua = Mojo::UserAgent;
$tx = $ua->start($tx);

Or just use the UserAgent post() method which will build and start the transaction for you.

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages