【问题标题】:400 Bad Request on XML POSTXML POST 上的 400 错误请求
【发布时间】:2023-06-14 06:50:01
【问题描述】:

我正在向 CISCO ISE 发送包含 XML 数据的 Post 请求,但收到以下错误:

 400 Bad Request         

我在 RestClient(用于 RESTful Web 服务)中检查了我的 XML 文本、标头和身份验证数据,它成功发出了 Post 请求。但是,我的请求在它的预期客户中失败了,我认为我的脚本有问题。

API 文档指出我应该拥有以下内容:

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 数据有问题吗?或者这个错误说明了什么?

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";
          }

【问题讨论】:

  • 您实际上并没有使用 REST::Client,即使您 use REST::Client;。你为什么不做my $client = REST::Client-&gt;new(...)
  • 服务器消息块 (SMB) 与此有什么关系?

标签: xml perl http-post cisco http-status-code-400


【解决方案1】:

您将 Content-Type 标头设置为 charset=utf-8

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

替换标头的先前值

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

charset=utf-8

你应该这样做

$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);

【讨论】:

  • 提示:如果您想避免使用引号,也可以使用Content_Type 代替'Content-Type'
  • 我根本不会指定字符集。虽然文本格式需要它,但 XML 和其他“应用程序”格式不需要它。
  • 感谢您的建议...但它不起作用,我仍然有错误 400 Bad Request 。我发出的文本 XML 是否正确?我的意思是我应该如何打包 (' 或只是 ',但实际上我已经尝试了所有...
  • @StayCalm 括号在这种情况下产生零差异。尝试打印 $res-&gt;decoded_content...也许 API 在响应正文中返回更详细的错误消息。