RSI_Don has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that I need to run which posts to a URL in order to retrieve a value. Encrypted strings are being passed, and decrypted ones are being returned. LWP has proven to be an inefficient (load/response-time) option, and so the next option is Curl. The issue is that no matter what I do, I am unable to suppress the headers from Curl. I need to just get the body of the response, not the headers. I have tried to do $curl->setopt(CURLOPT_HEADER, 0); with no success. Please see the code, and offer suggestions as to what I may do to resolve this issue.
#! /usr/bin/perl -w #"real" data changed to protect the guilty use strict; use WWW::Curl::Easy; use URI::Escape; my $curl = new WWW::Curl::Easy; my @strings = ("Encrypted String #1", "Encrypted String #2"); foreach my $encrypted (@strings) { my $query_string = "?action=decrypt&string=" . uri_escape($encrypte +d); $curl->setopt(CURLOPT_VERBOSE, 0); $curl->setopt(CURLOPT_HEADER, 0); $curl->setopt(CURLOPT_URL, 'https://www.somedomain.com/somescript.p +hp' . $query_string); # Starts the actual request $curl->perform; print "\n=-----------------------------------------=\n"; }
Here is a sample of the response I am getting back:
HTTP/1.1 200 OK Date: Thu, 29 May 2008 17:25:29 GMT Server: Apache/1.3.34 Ben-SSL/1.57 (Unix) PHP/4.4.2 X-Powered-By: PHP/4.4.2 Set-Cookie: PHPSESSID=716b50edcfb0291d088317c537f629ee; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre- +check=0 Pragma: no-cache Transfer-Encoding: chunked Content-Type: text/html string #1 =---------------------------------------------= HTTP/1.1 200 OK Date: Thu, 29 May 2008 17:25:29 GMT Server: Apache/1.3.34 Ben-SSL/1.57 (Unix) PHP/4.4.2 X-Powered-By: PHP/4.4.2 Set-Cookie: PHPSESSID=5a6b5de6dc36e01617a05b7bdbf8f384; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre- +check=0 Pragma: no-cache Transfer-Encoding: chunked Content-Type: text/html string #2 =---------------------------------------------=
Any thoughts on getting rid of those pesky headers?

Replies are listed 'Best First'.
Re: Cannot suppress Curl headers
by pc88mxer (Vicar) on May 29, 2008 at 18:56 UTC
    You have to set the CURLOPT_HEADERDATA and CURLOPT_WRITEDATA options. My testing indicates that their default value is STDOUT:
    my $response_data; my $response_header; open(my $RD, ">", \$response_data); open(my $RH, ">", \$response_header); ... $curl->setopt(CURLOPT_WRITEDATA, $RD); $curl->setopt(CURLOPT_HEADERDATA, $RH); ... $curl->perform; print "Response header: $response_header\n"; print "Response body: $response_body\n";
    Unforunately it doesn't seem you can use undef for these options.

    Some more testing indicates that CURLOPT_HEADER option determines whether or not the headers are copied through the CURLTOP_WRITEDATA handle as well as through the CURLOPT_HEADERDATA handle.

      Regrettably, those don't work. 1) use strict complains about those options. 2) I get 'use of uninitialized value in open' errors for opening the two filehandles. 3) $response_data comes back empty.
        What version of libcurl and WWW::Curl are you using? My test script works fine with use strict.

        Also, check your WWW::Curl documentation. I just installed the latest version, and that's where I got the idea about setting CURLOPT_WRITEDATA, etc.

Re: Cannot suppress Curl headers
by oko1 (Deacon) on May 29, 2008 at 20:43 UTC

    If you can't find anything in the docs or the code, then I'd say that you're justified in whacking it with a Perl clue-by-four. :)

    my $stuff = do { local $/; <DATA> }; $stuff =~ s/^.*?\n\n//s; print $stuff;
    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
Re: Cannot suppress Curl headers
by Anonymous Monk on May 30, 2008 at 08:44 UTC
    Why not check ->errbuf?
      I think you used $response_body, when you actually named it $response_data, if that helps.
        Oh I'm sorry I was referring to the post above with the example using WRITEDATA.