从 curl 响应中提取 HTTP body
Extract HTTP body from curl response
我使用包 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 body 而不提取 headers 的最佳方法是什么和东西。
现在我的回复是这样的:
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>
但我只想得到 body 没有 headers 的响应,所以它应该是这样的:
<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;
但它仍然包含 headers。
您可以使用 $curl->setopt(CURLOPT_HEADERDATA, $head)
和 $curl->setopt(CURLOPT_FILE, $body)
为来自响应的 header 和 body 数据设置单独的目标。
what is the best way to extract only HTTP body of that response
简单删除
$curl->setopt(CURLOPT_HEADER, 1);
或改为
$curl->setopt(CURLOPT_HEADER, 0);
如果你也想要header,那么还要添加以下内容:
$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.
我使用包 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 body 而不提取 headers 的最佳方法是什么和东西。
现在我的回复是这样的:
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>
但我只想得到 body 没有 headers 的响应,所以它应该是这样的:
<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;
但它仍然包含 headers。
您可以使用 $curl->setopt(CURLOPT_HEADERDATA, $head)
和 $curl->setopt(CURLOPT_FILE, $body)
为来自响应的 header 和 body 数据设置单独的目标。
what is the best way to extract only HTTP body of that response
简单删除
$curl->setopt(CURLOPT_HEADER, 1);
或改为
$curl->setopt(CURLOPT_HEADER, 0);
如果你也想要header,那么还要添加以下内容:
$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.