Hello,

I have created a simple webservice and I am now in the testing mode. I am trying to write a simple script that will use curl to create user accounts and another one to delete user accounts.

I have tried to use system to send curl commands

my $cmd = "c:\\curl\\curl -X PUT -d \"user[email]=email$_\" "
 + "-d \"user[password]=pass$_\" "
 + "http://localhost:3000/site/users/user$_";
print "create user failed\n" if system($cmd);

I've also tried using backtick

$userResponses[$_] =
  `c:\\curl\\curl -u user$_ -X DELETE http://localhost:3000/site/users/user$_`;

BTW, I have been using curl to manually test the service because it supports all of the HTTP methods instead of just GET and POST.

Anyway, the problem I am running into is that I can't figure out a way to interact with curl to provide the required password after sending a command that requires authorization. Is there a way to do this?

Any help would be greatly appreciated. I am open to suggestions, including ones that that don't depend on curl.

Thanks!

Recommended Answers

All 2 Replies

In case it makes a difference, is your platform *nix, Windows or Mac?

I don't know much about http or curl but before using the system command make sure the $cmd variable has been built successfully. You may want to use Perl's quote operator qq().

#!/usr/bin/perl -w
use strict;
my $cmd = "c:\\curl\\curl -X PUT -d \"user[email]=email$_\" "
 + "-d \"user[password]=pass$_\" "
 + "http://localhost:3000/site/users/user$_";
#For now, comment out the system command and see if $cmd contains correct string value
##print "create user failed\n" if system($cmd);
print $cmd;

Running the above gives a lot of error messages.

C:\Users\David\Programming\Perl>test.pl
Use of uninitialized value $_ in concatenation (.) or string at C:\Users\David\P
rogramming\Perl\test.pl line 3.
Use of uninitialized value $_ in concatenation (.) or string at C:\Users\David\P
rogramming\Perl\test.pl line 3.
...etc.......

Hi thanks for the reply. Perhaps I should have added a bit more detail. I am running in a windows vista 64bit environment, and the code snippets I provided actually are statements in a loop (that's why you are getting uninitialized value errors if you try to run them on their own).

So if you are trying to duplicate what I am experiencing you need to wrap the snippits in a loop like

for(0..n){
 <snippits go here>
}

However, the code I provided wasn't intended to be run outside of my specific test environment, as there are a many environmental conditions that you would have to try to match as well, which isn't really practical.

My question is more conceptual than anything. I realize that to do what I want I need to somehow split the processes so that perl isn't waiting for curl to return, but not really sure how to do that. I'm going to read up on the perlfork and open functions, hopefully I'll find a solution in one of those.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.