How to use put_p or put properly in the controller?

17 views
Skip to first unread message

Gordon Dollarnanda

unread,
Jul 15, 2020, 10:00:47 PM7/15/20
to Mojolicious

hi guys,

  Just started on a project with Mojolicious.

Task: to upload a file into mojo and then issue a PUT command to transfer that file to a file server.

I have been struggling to find documentation on using put_p/put in terms of the parameters for the syntax.

Often, looking at https://mojolicious.org/perldoc/Mojo/UserAgent#put_p, it is too brief and not concise.
 I have also tried to look for the source code in the Mojolicious framework which does the actual put_p/put to no avail. I had looked up the source of Mojo :: UserAgent and the packages it is "use"-ing also to no avail.

--------
   $self->ua->put_p(
           
           # my_target_url_object is a Mojo::URL with the
           # user info and actual url of the
           # file server defined.

           # $file_object is a Mojo::File::Asset representing a file I had uploaded from my local machine

            $my_target_url_object => form => {
                'content' => $file_object->asset->{'content'},
            }

        )->then(
        sub ($tx) {
            return $self->parse_response_p($tx);
        }
    )->catch(
        sub($error) {
            p->reject("Connection error: $error");
        }
    );
-----------
When I use the put call above,  the content of my input file becomes "content=+Why+Georgia%3F%0A" instead of just "Why Georgia" (which was what I had uploaded earlier).
I must be doing something wrong.

 I have since tried many variations  to the call to put_p to no avail.
Sometimes, if i do not provide the hash key, 'content', the actual cost when i cat the file in the target webserver directory would be literally 'HASH%3234234234';

Can any one please point me to proper documentation clearly describing how to execute a put/put_p in a  Mojolicious controller?

Thank you, guys


Gordon





Dan Book

unread,
Jul 15, 2020, 10:20:17 PM7/15/20
to mojol...@googlegroups.com
The URL you are PUTting to does not appear to expect a form, but raw content, so you will not want to use the "form" generator. You can pass raw content as a bare argument like so:

$self->ua->put_p($url, $file_object->asset->slurp);

You will probably also want to set a Content-Type header for the MIME type of the file contents. This would be set by a hash between the URL and content, there are some examples in https://metacpan.org/pod/Mojo::UserAgent#SYNOPSIS.

-Dan

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/19d370c9-81e3-4a52-aac9-254eef693bb8n%40googlegroups.com.

Gordon Dollarnanda

unread,
Jul 16, 2020, 12:18:48 AM7/16/20
to Mojolicious
hello Dan
 
Good day.
 Yea, i looked at the docs of Mojo::UserAgent again and found the following. Tried it and it works.


my $tx = $ua->build_tx(PUT => 'http://example.com' => {Accept => '*/*'} => 'Content!');


 And given i had user info credentials , I instantiated a Mojo::URL object with the userinfo() set with the credentials and it worked.

 Here's a test script to share:

#!/usr/bin/env perl

use Mojolicious::Lite;

use Mojo::UserAgent;

use Mojo::URL;

my $ua  = Mojo::UserAgent->new;


    my $url = Mojo::URL->new('http://127.0.0.1:9211/file_storage/stone_free.txt')->userinfo('dconway:IsperlRoyalty');

    my $res        = $ua->put( $url  => {Accept => '*/*'} =>  Mojo::File->new('/Users/hendrix/test-file.txt')->slurp  );



Reply all
Reply to author
Forward
0 new messages