I use package WWW::Curl::Easy
for API calls, and this is my example code:
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();
As you can see I save response into variable $response
, but I would like to know what is the best way to extract only HTTP body of that response, without headers and stuff.
Now my response looks like this:
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>
but I would like to get only body of that response without headers so it should be like this:
<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>
I tried stuff like:
$response_content = HTTP::Response->parse("$response") ;
$response_content = $response_content->content;
but it still contains headers.
CodePudding user response:
You can use $curl->setopt(CURLOPT_HEADERDATA, \$head)
and $curl->setopt(CURLOPT_FILE, \$body)
to set separate destination for header and body data from response.
CodePudding user response:
what is the best way to extract only HTTP body of that response
Simply remove
$curl->setopt(CURLOPT_HEADER, 1);
Or change it to
$curl->setopt(CURLOPT_HEADER, 0);
If you also also want the header, then also add the following:
$self->setopt(CURLOPT_HEADERDATA, \$head);
All together:
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.