2

I'm trying to send a GET request to Cisco ISE, but get this error:

415 Unsupported Media Typen

What am I doing wrong? The API documentation says:

Method: GET

URI: https://10.10.10.10:9060/ers/config/networkdevice/

HTTP 'Accept' header:application/vnd.com.cisco.ise.network.networkdevice.1.0+xml

So, I should be able to receive data as XML.

I've already tried to do this:

$header->header(
    'Content-Type'=>'application/vnd.com.cisco.ise.network.networkdevice.xml');
$header->header(
    'Content-Type'=>'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml');

Nothing works.. every time I get the above error.

Here's my full code.

use strict;
use warnings;
use JSON -support_by_pp;
use LWP 5.64;
use LWP::UserAgent;
use MIME::Base64;
use REST::Client;
use IO::Socket::SSL;
use HTTP::Headers;

#So dass es auch ohne SSL Sertifizierung funktioniert
BEGIN { $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0 }
#Create a user agent object
my $ua = LWP::UserAgent->new(ssl_opts=> {
SSL_verify_mode => SSL_VERIFY_NONE(),
verify_hostname => 0,
                                              }
                                        );

#Create a request
my $uri='https://10.10.10.10:9060/ers/config/networkdevice';
my $header = HTTP::Headers->new;
$header->header('Content-Type'=>'application/vnd.com.cisco.ise.network.networkdevice');
my $req = HTTP::Request->new('GET', $uri, $header);
$req-> authorization_basic("user", "user");

#Pass request to the user agent and get a response back
my $res = $ua->request($req);

#Check the outcome of the response
if ($res->is_success) {
  print $res->content;
} else {
   print $res->status_line, "n";
}
StayCalm
  • 145
  • 2
  • 13
  • Seems like it is not agreeing on the media type. –  Feb 16 '17 at 13:34
  • 1
    The docs say you need an `Accept` header, but you're sending a `Content-Type`. That's the wrong header. Writing an answer. – simbabque Feb 16 '17 at 13:37

2 Answers2

2

Your documentation says you should send an Accept header with that long type.

HTTP 'Accept' header:application/vnd.com.cisco.ise.network.networkdevice.1.0+xml

Accept is the content type(s) that your client will accept in the response. But you are using that as the Content-Type header in your GET request. This makes no sense, because a GET request has no body. Therefore, it has no content, and cannot have a type for its content.

The fact that the API responds with a 415 Unsupported Media Type is a bit strange. It could also just ignore that. We have no way to know why the developers chose to do that.

To fix your issue, change your request to use an Accept header instead of Content-Type.

my $header = HTTP::Headers->new;
$header->header(Accept => 'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml');
my $req = HTTP::Request->new('GET', $uri, $header);

Note you don't have to create your own HTTP::Headers object explicitly. For this simple request you can pass an array ref with key/value pairs to HTTP::Request->new.

my $req = HTTP::Request->new(
    'GET', 
    $uri, 
    [ Accept =>'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml' ],
);
simbabque
  • 53,749
  • 8
  • 73
  • 136
  • Seems like everything is in a prominent position, but unfortunatelly I have the same error...... Accept is not color ...do I need to install some modules? – StayCalm Feb 16 '17 at 14:06
  • @StayCalm can you add a dump of the request to your question please? You can do `print $req->as_string`. Just replace the basic auth data and anything else sensitive with something fake in the output before you post it here. – simbabque Feb 16 '17 at 14:08
  • `GET https://10.10.10.10:9060/ers/config/networkdevice Accept: application/vnd.com.cisco.ise.network.networkdevice+xml Authorization: Basic dXNlcjp1c2Vy ` – StayCalm Feb 16 '17 at 14:17
  • Is that the exact same string as the documentation has @StayCalm? I think the `1.0` is missing. I didn't copy it from your doc either. :-/ – simbabque Feb 16 '17 at 14:19
  • I've amended the answer to use the exact same string you showed from the documentation. – simbabque Feb 16 '17 at 14:20
  • 1
    @StayCalm it's always important to pay attention to details. You'll learn that over time. Sometimes it's a missing space, or a semicolon, or there's a line break to much. What I do in a case like that is copy/paste the sample data above the code I have, line them up and check if everything matches. – simbabque Feb 16 '17 at 14:29
0

Corrected code

#Create a request
my $uri='https://10.10.10.10:9060/ers/config/networkdevice';

my $req = HTTP::Request->new('GET', $uri);

$req->header('Accept'=>'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml');

$req->authorization_basic("user", "user");

#Pass request to the user agent and get a response back
my $res = $ua->request($req);

You have to send the Accept header not Content-Type.

Pradeep
  • 3,093
  • 17
  • 21