Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Sending a text file using POST

by Anonymous Monk
on Feb 11, 2016 at 14:51 UTC ( [id://1154961]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am trying to send text files ( using only one file here ) to a site's directory "https://www.tothesite.com/place/44498/", using POST.
I am getting a 404 error, but the url is correct. I think my problem is with the Perl, am I using the right way to use POST to send a file through HTTPS, any suggestion?
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use Data::Dumper; my $ua = LWP::UserAgent->new(); my $server_endpoint = "https://www.tothesite.com/place/44498/"; # set custom HTTP request header fields my $req = HTTP::Request->new(POST => $server_endpoint); $req->headers(); print Dumper( $req->headers() ); $req->header('x-auth-token' => 'Token e45ttkfksj48sdfj4jd9abcdd'); # add POST data to HTTP request body my $filename = "/var/www/location/first_text.txt"; $req->content($filename); my $resp = $ua->request($req); if ($resp->is_success) { my $message = $resp->decoded_content; print "Received reply: $message\n"; }else { print "HTTP POST error code: ", $resp->code, "\n"; print "HTTP POST error message: ", $resp->message, "\n"; }

Thanks for looking!

Replies are listed 'Best First'.
Re: Sending a text file using POST
by Corion (Patriarch) on Feb 11, 2016 at 14:53 UTC
    $req->header('x-auth-token' => 'Token e45ttkfksj48sdfj4jd9abcdd');

    Are you sure that the other side wants the header name all lowercase?

    $req->content($filename);

    This sends the filename, not the file content. Is that what you want?

    Also, if the server sends a 404, are you really, really sure that you're using the correct URL? Your URL ends with a slash, maybe you should leave that out? What other ways do you have to verify the correctness of the URL you use?

      The URL is 100% correct, tried with and without the slash.
      I need to send the file content not the file name, how do to about it, any suggestions?
      I updated the code to read the file content using "File::Slurp".
      #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use File::Slurp; use Data::Dumper; my $ua = LWP::UserAgent->new(); my $server_endpoint = "https://www.tothesite.com/place/44498/"; # set custom HTTP request header fields my $req = HTTP::Request->new(POST => $server_endpoint); $req->headers(); print Dumper( $req->headers() ); $req->header('x-auth-token' => 'Token e45ttkfksj48sdfj4jd9abcdd'); # add POST data to HTTP request body my $filename = "first_text.txt"; my $data_file = read_file( $filename, { binmode => ':raw' } ); $req->content($data_file); my $resp = $ua->request($req); if ($resp->is_success) { my $message = $resp->decoded_content; print "Received reply: $message\n"; }else { print "HTTP POST error code: ", $resp->code, "\n"; print "HTTP POST error message: ", $resp->message, "\n"; }

      Now I am getting this error:
      HTTP POST error code: 500
      Do you know if this line is code syntax correct?
       $req->header('x-auth-token' => 'Token e45ttkfksj48sdfj4jd9abcdd');

        I don't know. Maybe the documentation of the endpoint you're talking to has more information on what it expects?

Re: Sending a text file using POST
by choroba (Cardinal) on Feb 11, 2016 at 15:27 UTC
    The details depend on the service that runs on the given server. For example, we at work use something like
    #!/usr/bin/perl use warnings; use strict; use MIME::Base64 (); # Encoding of the authentication. use LWP::UserAgent; # Sending the request. use HTTP::Request::Common; # Creating the request. die "Specify File1 [File2...]\n" if @ARGV < 1; my $url = "http://..."; my $user = 'username'; my $password = 'passwd'; die "Cannot find the files specified.\n" if grep ! -f, @ARGV; my $auth_value = 'Basic ' . MIME::Base64::encode("$user:$password", q( +)); my $ua = 'LWP::UserAgent'->new; my $req = POST($url, Content_Type => 'multipart/form-data; charset=utf-8', Authorization => $auth_value, Content => [ Application => 'Testing', map { 'File_' . (1 + $_) => [ $ARGV[ +$_] ] } 0 .. $#ARGV) ]); my $response = $ua->request($req); if ($response->is_success) { print $response->content; } else { die 'ERROR: ', $response->status_line; }

    as the service expects File_1, File_2, File_3 etc. as POST parameters.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1154961]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-19 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found