DX NetOps

  • 1.  Spectrum REST API - output in json

    Posted Apr 18, 2014 10:56 AM

    Hi All,

    I am trying to get the data I am requesting from the REST API to return the data in json format instead of XML.  I found a small example in the documentatin, but I am unclear how to incorporate this into my code.

    Any tips or advice would be greatly appreciated.

    The example code:

    #!/usr/bin/perl

    use warnings;
    use LWP::UserAgent;

    my $ua = new LWP::UserAgent;


    my $message = '<?xml version="1.0" encoding="UTF-8"?>
    <rs:model-request throttlesize="10"
      xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
     <rs:target-models>
      <rs:models-search>
       <rs:search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
         <devices-only-search />
         <filtered-models>
           <equals>
            <attribute id="0x10009">
             <value>NetEngOnline</value>
            </attribute>
          </equals>
         </filtered-models>
       </rs:search-criteria>
      </rs:models-search>
     </rs:target-models>
     
    <!--
    Hostname
    IP Address
    Security String
    Model Type Name
    -->
       
    <rs:requested-attribute id="0x1006e" />
     <rs:requested-attribute id="0x12d7f" />
     <rs:requested-attribute id="0x10009" />
     <rs:requested-attribute id="0x10000" />
    </rs:model-request>';

    #print $message;

    my $req = new HTTP::Request 'POST','http://myspectrumserver.com:8080/spectrum/restful/models';
    $req->content_type('application/xml');
    $req->content($message);
    $req->authorization_basic('username', 'password');
          
    my $res = $ua->request($req);
    print $res->as_string;

     

     



  • 2.  RE: Spectrum REST API - output in json

    Posted Apr 21, 2014 11:46 PM
    susan_in_austin:

    Hi All,

    I am trying to get the data I am requesting from the REST API to return the data in json format instead of XML.  I found a small example in the documentatin, but I am unclear how to incorporate this into my code.

    Any tips or advice would be greatly appreciated.

    The example code:

    #!/usr/bin/perl

    use warnings;
    use LWP::UserAgent;

    my $ua = new LWP::UserAgent;


    my $message = '<?xml version="1.0" encoding="UTF-8"?>
    <rs:model-request throttlesize="10"
      xmlns:rs="http://www.ca.com/spectrum/restful/schema/request"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.ca.com/spectrum/restful/schema/request ../../../xsd/Request.xsd ">
     <rs:target-models>
      <rs:models-search>
       <rs:search-criteria xmlns="http://www.ca.com/spectrum/restful/schema/filter">
         <devices-only-search />
         <filtered-models>
           <equals>
            <attribute id="0x10009">
             <value>NetEngOnline</value>
            </attribute>
          </equals>
         </filtered-models>
       </rs:search-criteria>
      </rs:models-search>
     </rs:target-models>
     
    <!--
    Hostname
    IP Address
    Security String
    Model Type Name
    -->
       
    <rs:requested-attribute id="0x1006e" />
     <rs:requested-attribute id="0x12d7f" />
     <rs:requested-attribute id="0x10009" />
     <rs:requested-attribute id="0x10000" />
    </rs:model-request>';

    #print $message;

    my $req = new HTTP::Request 'POST','http://myspectrumserver.com:8080/spectrum/restful/models';
    $req->content_type('application/xml');
    $req->content($message);
    $req->authorization_basic('username', 'password');
          
    my $res = $ua->request($req);
    print $res->as_string;

     

     


    What OS ?



  • 3.  RE: Spectrum REST API - output in json

    Posted Apr 29, 2014 03:35 PM

    Spectrum is linux.  My test box is linux also.

    Susan



  • 4.  Re: Spectrum REST API - output in json

    Posted Jul 17, 2014 01:08 PM

    Set the Accept header to application/json

     

    E.g. with curl

     

    curl -X POST -H "Content-Type:application/xml" -H "Accept:application/json"



  • 5.  Re: Spectrum REST API - output in json
    Best Answer

    Posted Jul 17, 2014 03:41 PM

    Just add the appropriate header:


    my $req = new HTTP::Request 'POST','http://myspectrumserver.com:8080/spectrum/restful/models';

    $req->content_type('application/xml');

    $req->header('Accept' => 'application/json');

    $req->content($message);

    $req->authorization_basic('username', 'password');