libcpr / cpr

C++ Requests: Curl for People, a spiritual port of Python Requests.
https://docs.libcpr.org/
Other
6.52k stars 928 forks source link

Problems with HTTPS requests. #704

Closed 1Klas closed 2 years ago

1Klas commented 2 years ago

Description

The problem occurs when I'm trying to send any type of request to an https server. With http it works just fine. If server supports both, http and https it also works when I'm trying to send https request. To make sure that issue is actual I tried to install libcpr on another computer, and it did the same. Moreover, I used libcpr for a month and there were no problems ever, but as soon as I reinstalled the library, this happened. Also, I tried to test libCurl, and it sends https requests to any server.

Expected Behavior

Sending HTTPS requests as always.

Actual Behavior

Does send only HTTP requests and returns code 0 when trying to send HTTPS.

Your Environment

COM8 commented 2 years ago

I'd say this is a vcpkg issue since this depends on how you they build cpr. What happens, if you add the following code:

if(response.status_code == 0) {
std::cout << "Error code: " << response.error.code << '\n';
std::cout << "Error code: " << response.error.message << '\n';

}
1Klas commented 2 years ago

I'm also pretty sure it's vcpkg issue. Here's the result after running your code, I did it in a little other way, because "<<" operator isn't capable of operating with cpr::ErrorCode data type. Untitled

1Klas commented 2 years ago

I just downgraded the library to the older build, and it works smoothly, so the problem for me personally is closed. I'm waiting for your further instructions if I can help somehow.

COM8 commented 2 years ago

OK, what happens then in case you are doing only this:

if(response.status_code == 0) {
    std::cout << "Error code: " << response.error.message << '\n';
}
navaneeth-dev commented 2 years ago

OK, what happens then in case you are doing only this:

if(response.status_code == 0) {
    std::cout << "Error code: " << response.error.message << '\n';
}

Same issue. vcpkg, windows x64 static

int main()
{
    cpr::Response r = cpr::Get(cpr::Url{ "https://api.github.com/repos/whoshuu/cpr/contributors" },
        cpr::Authentication{ "user", "pass" },
        cpr::Parameters{ {"anon", "true"}, {"key", "value"} });
    std::cout << "Status code: " << r.status_code << '\n';
    std::cout << "Header:\n";
    if (r.status_code == 0) {
        //std::cout << "Error code: " << r.error.code << '\n';
        std::cout << "Error code: " << r.error.message << '\n';
    }
    for (const std::pair<std::string, std::string>& kv : r.header) {
        std::cout << '\t' << kv.first << ':' << kv.second << '\n';
    }
    std::cout << "Text: " << r.text << '\n';
    return 0;
}
Status code: 0
Header:
Error code:
Text:
navaneeth-dev commented 2 years ago

Any ideas how to fix?

navaneeth-dev commented 2 years ago

I just downgraded the library to the older build, and it works smoothly, so the problem for me personally is closed. I'm waiting for your further instructions if I can help somehow.

U downgraded via vcpkg.json ? Pls tell me the version number

tluchowski commented 2 years ago

I have the same problem.

After moving from cpr 1.6 to cpr 1.7.2 I am unable to do any HTTPS requests.

error.code is 4 error.message is empty

I'm also using it via vcpkg. When a time comes to update my vcpkg packages, I rename c:\vcpkg to c:\vcpkg.old and get a fresh vcpkg tree & build from git.

For downgrade, I just rename newly received c:\vcpkg to c:\vcpkg.new, and c:\vcpkg.old to c:\vcpkg and then I'm back where I was. I confirmed today going back to old vcpkg directory makes it work again.

My code is as follows:

cpr::Session session; cpr::SslOptions sslOpts = cpr::Ssl(cpr::ssl::NoRevoke{ true }); session.SetSslOptions(sslOpts); session.SetUrl(cpr::Url{ url }); session.SetBody(cpr::Body{ body }); session.SetHeader(cpr::Header{ { "Content-Type", "application/json" } }); session.SetTimeout(cpr::Timeout{ timeoutSec * 1000 }); session.SetVerifySsl(false);

auto request = session.Post();

COM8 commented 2 years ago

Closing this since this is a vcpkg issue. Please open an issue over at vcpkg since we can not do anything about this here sadly.

tluchowski commented 2 years ago

I've just reported this as https://github.com/microsoft/vcpkg/issues/23417

LilyWangLL commented 2 years ago

@COM8 The problem on VCPKG is due to curl upgrade from 7.80.0 to 7.81.0. Does cpr support version 7.81.0 of curl? I use libcurl-d.dll which installed by 7.80.0 of curl, this can output text of HTTPS correct. But when I use libcurl-d.dll which installed by 7.81.0 of curl, this will output error code 0.

LilyWangLL commented 2 years ago

Ping @COM8

COM8 commented 2 years ago
CMake Error at build/_deps/curl-src/CMakeLists.txt:362 (message):
[build]   The option CMAKE_USE_LIBSSH2 was renamed to CURL_USE_LIBSSH2.
[build] 
[build] 
[build] CMake Error at build/_deps/curl-src/CMakeLists.txt:362 (message):
[build]   The option CMAKE_USE_OPENSSL was renamed to CURL_USE_OPENSSL.
[build] 
[build] 
[build] CMake Error at build/_deps/curl-src/CMakeLists.txt:370 (message):
[build]   Reconfig required
[build] 
[build] 
[build] -- Configuring incomplete, errors occurred!

It looks like the named options changed in curl. I do not know too much about how vcpkg builds cpr, but this might be the cause here. Curl uses the new ones: https://github.com/microsoft/vcpkg/blob/e6509c997fef840f7cfdb84cf6e757b70ee283a8/ports/curl/portfile.cmake#L42-L44

But cpr uses the old ones: https://github.com/microsoft/vcpkg/blob/master/ports/cpr/001-cpr-config.patch

To fix this one could create a patch for cpr in vcpkg.

COM8 commented 2 years ago

I've created a patch https://github.com/libcpr/cpr/commit/742fccd48393c9233e93eeffcfac21e414af4b79 for it and in case nothing else comes up I can work on a new release of cpr next week.