CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Using curl

 



kepler
Novice

Aug 22, 2015, 1:51 AM

Post #1 of 9 (2288 views)
Using curl Can't Post

Hi,

I want to use in Perl the equivalente expression in Php:

curl -F "targets=@<filename>" "http://mydomain.com/script.php?<args>"

I provide a filename with text data (<filename>) and the args (<args>).

So it's a Post and Get request at the same time...

Any ideas?

Knd regards,

Kepler


Laurent_R
Veteran / Moderator

Aug 22, 2015, 2:45 AM

Post #2 of 9 (2282 views)
Re: [kepler] Using curl [In reply to] Can't Post

Cross posted on Perl Monks: http://www.perlmonks.org/?node_id=1139484.

Please note my comment just posted there:

Quote
To kepler: there nothing wrong at cross posting questions in various places, but is is usually considered polite to indicate that that you are posting or have posted the same question elsewhere and to provide the link, so that people in one location don't spend time answering a questions that has already been solved somewhere else.



kepler
Novice

Aug 22, 2015, 3:09 AM

Post #3 of 9 (2279 views)
Re: [Laurent_R] Using curl [In reply to] Can't Post

Hi Laurent,

Indeed you are right - sorry. But I always post my worst doubts in these two forums - they are the best around the Net.

Either way, if the policy is as you say, I'll be gladly to follow it.

Kind regards, and my apologies.

Kepler


FishMonger
Veteran / Moderator

Aug 22, 2015, 6:40 AM

Post #4 of 9 (2268 views)
Re: [kepler] Using curl [In reply to] Can't Post

At this point, I'll repeat the question asked by one of the monks.

Quote
So what is the question?


What have you tried?

How did it fail to meet your needs?


jkepler
Novice

Aug 22, 2015, 8:33 AM

Post #5 of 9 (2262 views)
Re: [FishMonger] Using curl [In reply to] Can't Post

Good afternoon,

I'm afraid I'm not able to access the Perl Monk website... Just this one (network connection issues, I think...).

Either way, I'm not an expert in the GET function in Perl. So all that I know, is to get a simple request like this for example:


Code
use LWP::Simple; 
print "Content-type: text/html\n\n";
$url='http://mydomain.com/script.php?arg1=2';
$out = (get $url) || (error("Can't connect to server at $url."));


This works with all my scripts. I send a url and get a text response - and then I handle the information.

BUT in this particular case, one of the arguments (targets) must be a text file uploaded at the same time as the request...

This is done with the curl command I've mentioned.

So...I'm stuck...

Regards,

JKepler


(This post was edited by jkepler on Aug 22, 2015, 8:35 AM)


FishMonger
Veteran / Moderator

Aug 22, 2015, 11:10 AM

Post #6 of 9 (2246 views)
Re: [jkepler] Using curl [In reply to] Can't Post

I have not tested this, but it's the starting point I'd use and adjust as needed.


Code
use HTTP::Request::Common; 
use LWP::UserAgent;

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

my $req = $ua->request( POST 'http://mydomain.com/script.php',
Content_Type => 'multipart/form-data',
Content => [ targets => [ 'filename' ] ]
);

my $response = $ua->request($req);



(This post was edited by FishMonger on Aug 22, 2015, 11:16 AM)


jkepler
Novice

Aug 22, 2015, 11:26 AM

Post #7 of 9 (2242 views)
Re: [FishMonger] Using curl [In reply to] Can't Post

Hi,

Thanks for the help Wink

I've tested the script, and got the message "You need a request object, not a HTTP::Response object" in line "my $response = $ua->request($req);"... Crazy

Regards,

JKepler


FishMonger
Veteran / Moderator

Aug 22, 2015, 11:46 AM

Post #8 of 9 (2240 views)
Re: [jkepler] Using curl [In reply to] Can't Post

I don't have time right now to setup a test environment for this, but try adjusting the statement to this:


Code
my $req = HTTP::Request->new( 
POST 'http://mydomain.com/script.php',
Content_Type => 'multipart/form-data',
Content => [ targets => [ 'filename' ] ]
);


If that fails, then read the documentation for both of the modules and adjust the syntax accordingly.


kepler
Novice

Aug 22, 2015, 3:41 PM

Post #9 of 9 (2229 views)
Re: [FishMonger] Using curl [In reply to] Can't Post

Hi,

I've just tested your kind sugestion. I get the following result:

Using the code:

Code
my $response = $ua->request($req); 
my $message = $response->decoded_content;
print $response;
print "Received reply: $message\n";


I get:

HTTP::Response=HASH(0x1b56434)
Received reply: 400 URL missing

So the problem is really a URL that I must provide.

Regards

JKepler


(This post was edited by kepler on Aug 23, 2015, 1:12 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives