cpr icon indicating copy to clipboard operation
cpr copied to clipboard

Even with cpr::VerifySsl(false), HTTPS requests always return an empty string

Open WesThorburn opened this issue 6 years ago • 3 comments

Sample Code:

auto r = cpr::Get(cpr::Url{ "https://youtube.com" },cpr::VerifySsl(false));
std::cout << r.url << std::endl; 
std::cout << r.status_code << std::endl; // 200
std::cout << r.text << std::endl;

Output:

https://youtube.com/
0


The above output is identical even if spr::VerifySsl(false) is removed. Request works fine if using HTTP.

I'm running Ubuntu 16.04, 64 bit, however several of other users have been reporting this same issue on a variety of machines. Issue 261 Issue 254 Issue 246 Issue 222 Issue 194

WesThorburn avatar Mar 12 '18 21:03 WesThorburn

Hello, can you share the error you got? Class cpr::Response contains public member error of type cpr::Error with two fields code which contains enum and message with text representation of error, do std::cout << r.error.message << std::endl; or watch through debugger to retrieve actual error you has.

Liastre avatar Mar 28 '18 10:03 Liastre

I had the same issue and found the problem: Accessing VerifySSL from global namespace doesn't change anything on the CURL attributes. You should directly use session class to change verification of the SSL like below:

    auto url = cpr::Url{"http://www.httpbin.org/get"};
    auto parameters = cpr::Parameters{{"hello", "world"}};
    cpr::Session session;
    session.SetUrl(url);
    session.SetParameters(parameters);
    session.SetVerifySsl(false);
    
    auto r = session.Get();

Edit: Actually, there is another and much simpler way to do that:

    auto response = cpr::Get(cpr::Url{"https://httpbin.org/get"}, 
                                cpr::VerifySsl{false});
    std::cout << "Status Code: " << response.status_code << std::endl;
    std::cout << response.text << std::endl;

I think documentation is really very basic and needs to be updated.

emrahayanoglu avatar Jun 05 '18 10:06 emrahayanoglu

@emrahayanoglu could you create a PR for the gh-pages branche to update documentation for this please?

COM8 avatar Apr 16 '20 13:04 COM8

Is this issue still relevant, or is CPR not supposed to work on Ubuntu (I tried two different versions)? I tried building (find_package() variant) and running an example from the homepage without success. The r.status_code for the provided example is 0 all the time.

Linux Mint 21, Ubuntu 20.04

Nullarity avatar Apr 01 '23 16:04 Nullarity

@Nullarity we even test against multiple different versions of Ubuntu in our CI to make sure everything works on Ubuntu (https://github.com/libcpr/cpr/blob/master/.github/workflows/ci.yml).

Usually behaviour like this is caused by wrong CA paths. What you could try is setting CURL_CA_FALLBACK like I have done it here: https://github.com/COM8/home-ui/blob/dfa172c24a835d75c3940d5e9084b995cebf3b83/CMakeLists.txt#L110-L114

COM8 avatar Apr 02 '23 17:04 COM8

You could also try passing cpr::Verbose{true} to a session (or a request) to get more debug output from curl. If you decide to do this, please post the output here.

COM8 avatar Apr 02 '23 17:04 COM8

@COM8 Sorry for the late reply. cpr::Verbose{true} helped to identify this problem: Protocol "https" not supported or disabled in libcurl. I compiled openssl from source, then recompiled cpr and finally solved the problem. Thanks for your help.

Nullarity avatar Apr 18 '23 21:04 Nullarity

Awesome. Great to hear @Nullarity ! Closing this issue for now. Feel free to open a new one in case anyone still has issues with it.

COM8 avatar Apr 22 '23 12:04 COM8

Hello! Same issue for me. I got CPR from VCPKG, my OS is Manjaro Linux. Verbose doesn't show anything (or anything that im aware of) curl returns a string that i've set to be returned, cpr doesnt.

Code:

    nlohmann::json data;
    for (int i = 1; i+1 < line->size(); ++i) {
        if (line->at(i+1).find("https") == string::npos) {
            data[line->at(i)] = line->at(i+1);
        }
    }
    cout << data.dump() << endl;
    cpr::Response response = cpr::Post(cpr::Url{line->at(line->size()-1)},
                                        cpr::Body{data.dump()},
                                        cpr::VerifySsl(false));
    cout << response.text << endl;

Verbose output:

  * WARNING: failed to open cookie file ""
  *   Trying 
  * Connected to port 443 (#0)
  * ALPN: offers h2,http/1.1
  * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
  * ALPN: server accepted http/1.1
  * Server certificate:
  *  subject: CN=
  *  start date: May 22 14:14:12 2023 GMT
  *  expire date: Aug 20 14:14:11 2023 GMT
  *  issuer: C=US; O=Let's Encrypt; CN=R3
  *  SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
  * using HTTP/1.1
  > POST /api HTTP/1.1
  Host: 
  User-Agent: curl/8.1.0
  Accept: */*
  Content-Length: 15
  Content-Type: application/x-www-form-urlencoded
  
  * old SSL session ID is stale, removing
  < HTTP/1.1 200 OK
  < Date: Thu, 08 Jun 2023 19:59:22 GMT
  < Server: Apache
  < Expires: Thu, 19 Nov 1981 08:52:00 GMT
  < Cache-Control: no-store, no-cache, must-revalidate
  < Pragma: no-cache
  < Access-Control-Allow-Origin: *
  < Access-Control-Allow-Headers: *
  < Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
  * Added cookie ci_sessions="ub3hs5frho0btgc18cei079l7u7b74k5" for domain , path /, expire 1686261541
  < Set-Cookie: ci_sessions=ub3hs5frho0btgc18cei079l7u7b74k5; expires=Thu, 08-Jun-2023 21:59:22 GMT; Max-Age=7200; path=/; domain=; HttpOnly
  < Upgrade: h2,h2c
  < Connection: Upgrade
  < Content-Length: 0
  < Content-Type: text/html; charset=UTF-8
  < 
  * Connection #0 to host  left intact

Curl:
   curl --data-urlencode "type=test"                                                                                                                                            
   AAAA

okadapy avatar Jun 08 '23 20:06 okadapy

Try replacing cpr::Body with cpr::Payload.

COM8 avatar Jun 09 '23 16:06 COM8