2017-02-22 28 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