asafd
asafd

Reputation: 360

getting status 405 from rest service when using perl REST::Client

I am using Perl 5.16 with REST::Client.

my code looks like this:

my $client = REST::Client->new();

$client->setHost($server_address);

my $url = "www.myservice.com/rest/do";

my $json = JSON->new;
my $json_request = $json->encode($request);

$client->addHeader("x-request-header", $base64Header);
$client->addHeader("Content-Type", "application/json");

$client->POST($url, $json_request);

This code always produces HTTP status 405 (Method not allowed).

However, when I simply use curl with the same details, the response is successful:

sendCURL($client->getHost().$url, $json_request, $base64Header);

sub sendCURL {
  my $url = $_[0];
  my $data = $_[1];
  my $header = $_[2];

  my $curl = "curl --data \'".$data."\' --header \"Content-Type: application/json\" --header \"x-request-header: ".$header."\" ".$url;
  print $curl."\n";
  my $response = system $curl;
  print $response."\n";
}

Note that the curl request is definitely a POST (--data parameter causes a POST request) and I know for sure that the service is expecting POST.

It must be a problem in the perl module REST::Client. Maybe I'm using it wrong?

Upvotes: 2

Views: 367

Answers (2)

Deano
Deano

Reputation: 12190

I know this is an older post "again time is relative"!

I have stumbled upon the same issue, my solution was to change request method from POST to PUT and it did it.

Upvotes: 0

Len Jaffe
Len Jaffe

Reputation: 3484

Do you have access to the server logs?

The curl request is probably a GET, not a POST.

Do you have API documentation? Does it say to use POST?

If it does, then it's a server configuration issue. If the API docs do not say to use POST then it's you.

Upvotes: 1

Related Questions