Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Attempting to POST a file to local server

by cookersjs (Acolyte)
on May 10, 2017 at 18:39 UTC ( [id://1190003]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, Hope you are well!

I am attempting to POST a text file to a local server, using port 8012. I have been trying to do this using LWP::UserAgent. The main example I have been trying is found here.
Based on the example, I have produced the following code:

my $ua = LWP::UserAgent->new; my $res = $ua->post('http://localhost:8012/', Content_Type => 'form_data', Content => [ Filedata => [ 'Hello.txt', 'Hello.txt', Content_Type => 'text/plain', ], submit => 'Submit', ], ); if ($res->is_success) { print $res->content; } else { print $res->status_line, "\n"; }
To get more specific about the codes purpose: I am running a docker container on a server that is communicated with via port 8012. The goal is to send a file here, process it and then return the
body of the processed file. When I use Postman to submit the file via POST request, it works as expected: The server gets the file, processes it, and returns the body of the desired output file.

When I run the above code, I get "TypeError: Cannot read property 'end' of undefined" on the server end, and the output from the program is '500 Server closed connection without sending any
data back'. This is my first time doing any of this sort of thing, so any tips, solutions, and critiques are obviously welcome!

cookersjs


Replies are listed 'Best First'.
Re: Attempting to POST a file to local server
by Corion (Patriarch) on May 10, 2017 at 18:54 UTC

    The error happens on the remote end, so it's hard to say what the remote end is getting from "postman" that is missing from your code.

    Maybe the file Hello.txt doesn't exist where you think it should?

    Maybe you can dump the request that is sent from your Perl code (see LWP::Debug) and compare it to the data that "postman" sends.

Re: Attempting to POST a file to local server
by cookersjs (Acolyte) on May 10, 2017 at 18:55 UTC
    Well this was fun but I found out why! Turns out an underscore in the 'Content_Type' field rather than a dash makes both Perl and the server unhappy

    Best -- cookersjs

      But the code in your original post already has an underscore.

        I too found the phrasing confusing but it is correct as stated

        A value of form_data (an error) is treated as "application/x-www-form-urlencoded"

        A value of form-data is treated as "multipart/form-data"

        #!/usr/bin/perl -- use strict; use warnings; use LWP; my $ua = LWP::UserAgent->new; $ua->add_handler("request_send", sub { shift->dump; return }); $ua->add_handler("response_done", sub { shift->dump; return }); $ua->timeout( 0.1 ); my $res = $ua->post('http://localhost:8012/', #~ Content_Type => 'form_data', ## treated as "application/x-www-f +orm-urlencoded" Content_Type => 'form-data', ## treated as "multipart/form-data" #~ Content_Type => 'multipart/form-data', Content => [ Filedata => [ __FILE__, 'Hello.txt', Content_Type => 'text/plain', ], submit => 'Submit', ], );

        BTW: Nice that you are back.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        Furthermore I consider that Donald Trump must be impeached as soon as possible

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found