Postman 2 Mojo

51 views
Skip to first unread message

Pierre Pannetier

unread,
Sep 30, 2016, 4:51:20 AM9/30/16
to Mojolicious

Hi


I'm trying to post 2 files to an API build with Mojo, testing it with Postman

Well doing this, the only way i found to get files content is to split $string with regular expressions

my $asset = $self->req->content->asset;
my $string=$asset->slurp;


I tried Mojo::Upload; without success.

Anybody could drive me to get each file content without playing with regular expressions ?

Many thx.











Juergen Nickelsen

unread,
Sep 30, 2016, 5:59:46 AM9/30/16
to mojol...@googlegroups.com
On 30.09.2016 10:51, Pierre Pannetier wrote:
> Well doing this, the only way i found to get files content is to split
> $string with regular expressions
>
> my$asset =$self->req->content->asset;
> my$string=$asset->slurp;
>
> I tried Mojo::Upload; without success.
>
> Anybody could drive me to get each file content without playing with
> regular expressions ?

To make sure I understand you correctly: Do you have the byte stream of
the file in $string as a single string, as using
Mojo::Asset::Memory::slurp() would suggest?

If so, splitting it on line ends to get an array of lines as in

@lines = split(/\n/, $string);

is indeed the way this is meant to be done (and not really what I'd call
playing with regexps).

Regards, Juergen.

--
<Juergen....@fu-berlin.de> Tel +49.30.838-50740 Fax -450740
Zentraleinrichtung fuer Datenverarbeitung, Systems Impetulant
Freie Universitaet Berlin, Fabeckstrasse 32, 14195 Berlin, DE

Pierre Pannetier

unread,
Sep 30, 2016, 8:39:33 AM9/30/16
to Mojolicious
Actually this what i have from the $asset->slurp;

------WebKitFormBoundary2jXzI9WLBRoANocy
Content-Disposition: form-data; name="text"; filename="test.json"
Content-Type: application/octet-stream

 
{
JSON
 
}
------WebKitFormBoundary2jXzI9WLBRoANocy
Content-Disposition: form-data; name="filepdf"; filename="test(2).PDF"
Content-Type: application/pdf

%PDF-1.4
...pdf data....
%%EOF

So in the case i have to split et manage to get the files content using regexps.

But i'm wondering it is possible to get this through something like

my $pdfdata = $self->param('filepdf');
my $jsondata = $self->param('text');


Brian Manning

unread,
Sep 30, 2016, 11:02:31 AM9/30/16
to Mojolicious


On Friday, September 30, 2016 at 5:39:33 AM UTC-7, Pierre Pannetier wrote:
But i'm wondering it is possible to get this through something like

my $pdfdata = $self->param('filepdf');
my $jsondata = $self->param('text');

The "param" method of your Mojolicious::Controller object should work [1] for accessing your POSTed files, but you can also try retrieving the files directly from the Mojo::Message object [2] inside your controller;

# assuming '$self' isa Mojolicious::Controller object...
my $pdfdata = $self->req->upload('filepdf');
my $jsondata = $self->req->upload('text');

The above syntax is working for me uploading single files from iOS (NSURLConnection) into Mojolicious.  There's also a small mention of uploads in the Mojolicious Tutorial [3].

Thanks,

Brian

Pierre Pannetier

unread,
Oct 3, 2016, 6:20:30 AM10/3/16
to Mojolicious
Hi Brian ,

Thanks for your answer, i tried it, but this returns empty values for me.


my $pdfdata = $self->req->upload('filepdf');
my $jsondata = $self->req->upload('text');
print "JSONDATA:".$jsondata."\n";
print "PDFDATA:".$pdfdata."\n";

Use of uninitialized value $jsondata in concatenation (.) or string at ./test.pl line 645.
JSONDATA
:
Use of uninitialized value $pdfdata in concatenation (.) or string at ./test.pl line 646.
PDFDATA
:

I'm still wondering why...

Brian Manning

unread,
Oct 3, 2016, 9:36:53 AM10/3/16
to Mojolicious


On Monday, October 3, 2016 at 3:20:30 AM UTC-7, Pierre Pannetier wrote:
Hi Brian ,

Thanks for your answer, i tried it, but this returns empty values for me.

I'm still wondering why...

Maybe create a small demo Mojolicious::Lite app that demonstrates your problem, so that others can test it with Postman as well?

FWIW, it took me a while to format the POST message correctly before Mojolicious would decode it.

Thanks,

Brian 
Reply all
Reply to author
Forward
0 new messages