http://qs1969.pair.com?node_id=1154962


in reply to Sending a text file using POST

$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?

Replies are listed 'Best First'.
Re^2: Sending a text file using POST
by Anonymous Monk on Feb 11, 2016 at 14:58 UTC
    The URL is 100% correct, tried with and without the slash.
Re^2: Sending a text file using POST
by Anonymous Monk on Feb 11, 2016 at 15:16 UTC
    I need to send the file content not the file name, how do to about it, any suggestions?
Re^2: Sending a text file using POST
by Anonymous Monk on Feb 11, 2016 at 15:30 UTC
    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?

        I even added this line before the "header":
        $req->content_type('multipart/form-data');
        And still an error; multi-part form data parse error.