2017-02-22 83 views
0

我发送POST请求思科ISE使用XML数据和我收到以下错误:400对XML POST请求无效

400 Bad Request   

我已经检查了我的XML文本,标题和认证数据的RESTClient实现(对于REST风格的Web服务),它成功发布了Post请求。但是,我的要求与其预期的客户端一致,我相信我的脚本有问题。

Method: POST 
URI: https://10.10.10.10:9060/ers/config/networkdevice 
HTTP 'Content-Type' header:application/vnd.com.cisco.ise.network.networkdevice.1.0+xml; charset=utf-8  
HTTP 'Accept' header: application/vnd.com.cisco.ise.network.networkdevice.1.0+xml 

SMB能告诉我这是不对我的XML数据:

,我应该有以下的API文档的状态?或者这个错误是什么意思?

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; 
use HTTP::Request; 
use XML::Simple; 

#Create a user agent object 
    my $ua = LWP::UserAgent->new(ssl_opts=> { 
    SSL_verify_mode => SSL_VERIFY_NONE(), 
    verify_hostname => 0, 
    } 
    ); 


my $uri='https://10.10.10.10:9060/ers/config/networkdevice/'; 
my $header = HTTP::Headers->new; 
my $req = HTTP::Request->new('POST', $uri); 

my $message =('<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <ns3:networkdevice name="LAB-WLZZ" id="89c7e480-591f-11e4-ac6a b83861d71000" xmlns:ns2="ers.ise.cisco.com" xmlns:ns3="network.ers.ise.cisco.com"> 
    <authenticationSettings> 
     <enableKeyWrap>false</enableKeyWrap> 
     <keyInputFormat>ASCII</keyInputFormat> 
     <networkProtocol>RADIUS</networkProtocol> 
     <radiusSharedSecret>******</radiusSharedSecret> 
    </authenticationSettings> 
     <NetworkDeviceIPList> 
     <NetworkDeviceIP> 
      <ipaddress>9.9.9.9</ipaddress> 
      <mask>21</mask> 
     </NetworkDeviceIP> 
     </NetworkDeviceIPList> 
     <NetworkDeviceGroupList> 
     <NetworkDeviceGroup>Location</NetworkDeviceGroup> 
     <NetworkDeviceGroup>DeviceType</NetworkDeviceGroup> 
     </NetworkDeviceGroupList> 
    </ns3:networkdevice>') 
    ; 

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


    $req->content($message); 
    $req->content_type("charset=utf-8"); 
    $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->status_line, "n"; 
      } else { 
       print $res->status_line, "n"; 
      } 
+0

你实际上并没有使用REST ::客户,即使你'使用REST ::客户端;'。你为什么不在'my $ client = REST :: Client-> new(...)'? – ThisSuitIsBlackNot

+0

什么服务器消息块(SMB)有这个做什么? – Borodin

回答

4

你设置Content-Type头,以charset=utf-8

$req->content_type("charset=utf-8"); 

取代头与

charset=utf-8 

application/vnd.com.cisco.ise.network.networkdevice.1.0+xml 

前值

你应该做

$req->content_type(
    'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml; charset=utf-8' 
); 

您也可以建立自己的要求是这样的:

my $req = HTTP::Request->new('POST', $uri, [ 
    Accept   => 'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml', 
    'Content-Type' => 'application/vnd.com.cisco.ise.network.networkdevice.1.0+xml; charset=utf-8' 
], $message); 
+1

提示:'Content_Type'也可以,如果你想避免引号代替''内容Type''。 – ikegami

+0

我不会指定一个字符集。虽然它需要文本格式,但不适用于XML和其他“应用程序”格式。 – ikegami

+0

感谢您的建议...但它好好尝试一下工作,我还是错误'400错误Request'。我的文本XML是否正确?我的意思是我应该怎么收拾那个'(“'或'只是”',但实际上我已经试过一切... – StayCalm