Can I do an HTTP DELETE in Perl?

M

mhubbard

I'm trying to test a REST web service and need to use the HTTP DELETE
method. It doesn't appear to be supported in LWP::UserAgent. Is it
possible to do this in Perl?
 
P

Paul Lalli

mhubbard said:
I'm trying to test a REST web service and need to use the HTTP DELETE
method. It doesn't appear to be supported in LWP::UserAgent.

By "not supported", I assume you mean there isn't a method which does
it directly. There is, however, the generic request() method, which
takes an object of the HTTP::Request class. Reading the docs on this
class, there does not seem to be any restriction on which HTTP method
you use to construct your request.

I would suggest trying that.

Paul Lalli
 
M

mhubbard

This is what I have tried:

my $URL = "$protocol\://$host:$port/sdk/device?ip=$deviceDELETEIP";
my $request = HTTP::Request->new('DELETE', $URL);
$response = $browser->request($request);

I don't get any errors but I don't see any evidence that it is doing
anything either.

print $response->content; gives me nothing back.
 
P

Paul Lalli

mhubbard said:
This is what I have tried:

my $URL = "$protocol\://$host:$port/sdk/device?ip=$deviceDELETEIP";
my $request = HTTP::Request->new('DELETE', $URL);
$response = $browser->request($request);

I don't get any errors but I don't see any evidence that it is doing
anything either.

print $response->content; gives me nothing back.

Please quote some context when posting a reply.

Surely at least your webserver logs that a request came in? What do
you find in the server's access and error logs?

Paul Lalli
 
M

mhubbard

my $URL = "$protocol\://$host:$port/sdk/device?ip=$deviceDELETEIP";
my $request = HTTP::Request->new('DELETE', $URL);
$response = $browser->request($request);


My server does not log any request coming in.

I can put any value as the first parameter to HTTP::Request->new
without throwing an error. Even though print $request->as_string;
clearly prints DELETE URL_STRING, I get "Can't call method 'request' on
an undefined value" when I try to pass $request in
$response->$browser->request($request); $browser is an LWP::UserAgent
object.

Pardon my lack of context. I am quite a novice at Perl and to posting
to your groups. I am open to any suggestions. I will probably be
posting frequently now that I finally have the opportunity to write
some Perl, so I definitely want to follow protocol.

Thanks,
Mandy
 
K

Keith Keller

Please post a short but *complete* script which replicates the problem.
(In some cases this is not easy, but in yours it really should be.)
Pardon my lack of context. I am quite a novice at Perl and to posting
to your groups. I am open to any suggestions. I will probably be
posting frequently now that I finally have the opportunity to write
some Perl, so I definitely want to follow protocol.

Then please read the Posting Guidelines which are posted here regularly.

--keith
 
K

Keith Keller

Then please read the Posting Guidelines which are posted here regularly.

....and don't multipost, to comp.lang.perl.moderated or any other
newsgroup. If you must reach multiple groups, first reconsider, then
crosspost rather than multipost.

--keith
 
M

mhubbard

Okay, let me try this again. I have read the documentation for
LWP::UserAgent, HTTP::Request, and HTTP::Response. None of these
explicitly state that HTTP delete can be used. My server logs nothing.
It is as if the URL was never even hit.


My Code:
sub _deviceDELETEbyIP
{
($self, $host, $user, $pswd, $port, $protocol, $deviceDELETEIP)=@_;

my $FormLogin = FormLogin->new();


#This method returns an LWP::UserAgent object
$browser=$FormLogin->doLogin($host, $user, $pswd, $port, $protocol);

#This is a URL to hit a web service on my server
my $URL = "$protocol\://$host:$port/sdk/device?ip=$deviceDELETEIP";

my $request = HTTP::Request->new('DELETE', $URL);

# Prints DELETE https://SERVER:443/sdk/device?ip=10.100.1.20
print $request->as_string();


# I get an error- Can't call method "request" on an undefined value
my $response=$browser->request($request);


}

END

I have written several methods that perform other http methods. I just
can't get the DELETE method to work. I'm wondering if it is even a
valid option? Any help would be greatly appreciated.

Thanks,
Mandy
 
A

A. Sinan Unur

Okay, let me try this again. I have read the documentation for
LWP::UserAgent, HTTP::Request, and HTTP::Response. None of these
explicitly state that HTTP delete can be used. My server logs
nothing. It is as if the URL was never even hit.


My Code:
sub _deviceDELETEbyIP
{
($self, $host, $user, $pswd, $port, $protocol, $deviceDELETEIP)=@_;

my $FormLogin = FormLogin->new();

#This method returns an LWP::UserAgent object
$browser=$FormLogin->doLogin($host, $user, $pswd, $port, $protocol);

Well, apparently it doesn't return what you imagine it returns, does it?
This is why the posting guidelines ask you to do some work by
constructing the shortest possible script that still exhibits the
problem. This is a problem-solving technique that is exceptionally
effective.

It looks like you are not using strict and warnings, which means you
might have screwed up any part of the code you are not showing us, and
there is no way for anyone to diagnose it.
#This is a URL to hit a web service on my server
my $URL = "$protocol\://$host:$port/sdk/device?ip=$deviceDELETEIP";

my $request = HTTP::Request->new('DELETE', $URL);

# Prints DELETE https://SERVER:443/sdk/device?ip=10.100.1.20
print $request->as_string();

# I get an error- Can't call method "request" on an undefined value
my $response=$browser->request($request);

Of course the URL won't be hit because you do not have a valid UserAgent
object to submit the request through.
I have written several methods that perform other http methods. I
just can't get the DELETE method to work. I'm wondering if it is even
a valid option?

Who knows. Check your server documentation.

Try the following, see what it reports. Read and follow the posting
guidelines. After you run it, check both the access and error logs on
the web server.

#!/usr/bin/perl

use strict;
use warnings;

use HTTP::Request;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->agent('HTTP_DELETE_TESTER');

my $req = HTTP::Request->new(
DELETE => 'https://SERVER:443/sdk/device?ip=10.100.1.20'
);

my $res = $ua->request($req);
if ($res->is_success) {
print $res->decoded_content;
} else {
print "Error: " . $res->status_line . "\n";
}
__END__
 
B

Bart Lateur

mhubbard said:
I get "Can't call method 'request' on
an undefined value" when I try to pass $request in
$response->$browser->request($request); $browser is an LWP::UserAgent
object.

But what a weird line of code that is. I have no idea what it should
look like, but I'm 99.99% certain that this is not it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top