我使用包WWW::Curl::Easy進行 API 呼叫,這是我的示例代碼:
use WWW::Curl::Easy;
my $curl = WWW::Curl::Easy->new();
$curl->setopt(CURLOPT_POST, 1);
$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_HTTPHEADER, ['Accept: text/xml; charset=utf-8', 'Content-Type:text/xml; charset=utf-8', 'SOAPAction: "importSheet"']);
$curl->setopt(CURLOPT_POSTFIELDS, $requestMessage);
$curl->setopt(CURLOPT_URL, $tom::{'setup'}{'api'}{'carrier'}{'url'});
my $response;
$curl->setopt(CURLOPT_WRITEDATA, \$response);
main::_log(Dumper(\$curl));
my $ret = $curl->perform();
如您所見,我將回應保存到變數中$response,但我想知道僅提取該回應的 HTTP 正文的最佳方法是什么,而沒有標頭和其他內容。
現在我的回復是這樣的:
HTTP/1.1 500 Internal Server Error
Date: Fri, 26 Nov 2021 21:38:42 GMT
Content-Type: text/xml
Connection: keep-alive
Content-Length: 241
Set-Cookie: TS01972c9d=01a27f45ea407d6a9622e8d70528d3201676317364865a22da7d73be308d9e49021a872fbfe71877fbee80ce454071bc9a105a4e33; Path=/; Domain=.test.test.com
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>user_not_found</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
但我只想得到沒有標題的回應正文,所以它應該是這樣的:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>SOAP-ENV:Server</faultcode><faultstring>user_not_found</faultstring></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
我試過這樣的東西:
$response_content = HTTP::Response->parse("$response") ;
$response_content = $response_content->content;
但它仍然包含標題。
uj5u.com熱心網友回復:
僅提取該回應的 HTTP 正文的最佳方法是什么
只需洗掉
$curl->setopt(CURLOPT_HEADER, 1);
或者改成
$curl->setopt(CURLOPT_HEADER, 0);
如果您還需要標題,則還要添加以下內容:
$self->setopt(CURLOPT_HEADERDATA, \$head);
全部一起:
my $curl = WWW::Curl::Easy->new();
$curl->setopt(CURLOPT_POST, 1);
$curl->setopt(CURLOPT_URL, ...);
$curl->setopt(CURLOPT_HTTPHEADER, ...);
$curl->setopt(CURLOPT_POSTFIELDS, ...);
# $curl->setopt(CURLOPT_HEADER, 0); This is the default.
$curl->setopt(CURLOPT_WRITEDATA, \my $body); # If you want the body.
$curl->setopt(CURLOPT_HEADERDATA, \my $head); # If you want the head.
uj5u.com熱心網友回復:
您可以使用$curl->setopt(CURLOPT_HEADERDATA, \$head)和$curl->setopt(CURLOPT_FILE, \$body)為回應中的標頭和正文資料設定單獨的目標。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368135.html
上一篇:處理不明確的處理程式方法映射約定
下一篇:網路服務器有時無法發送所有影像
