yhirose / cpp-httplib

A C++ header-only HTTP/HTTPS server and client library
MIT License
12.97k stars 2.29k forks source link

Client POST fails on getaddrinfo #1247

Closed leonuetzel closed 2 years ago

leonuetzel commented 2 years ago

Hello,

im trying to connect to ETSY API v3 to obtain an Access Token with OAuth2 Protocol: https://developer.etsy.com/documentation/essentials/authentication/ The Problem is, that i need to make a POST Request to "https://api.etsy.com" but getaddrinfo wont open a Socket and returns INVALID_SOCKET, which makes everything after fail. Exactly like here: https://githublab.com/repository/issues/yhirose/cpp-httplib/1225 only that i got a (hopefully) valid URL. I dont understand what im doing wrong.

`std::string url("https://api.etsy.com"); httplib::SSLClient client(url);

    std::map<std::string, std::string> parameters;
    parameters["grant_type"     ] = "authorization_code";
    parameters["client_id"      ] = m_apiKey;
    parameters["redirect_uri"   ] = "https://localhost:443";
    parameters["code"                   ] = code;
    parameters["code_verifier"] = verifier_base64;

    std::string parameterString;
    bool first = true;
    for(auto& i: parameters)
    {
        if(first == false)
        {
            parameterString += "&";
        }
        parameterString += i.first + "=" + i.second;
        first = false;
    }

    httplib::Result result = client.Post("/v3/public/oauth/token", parameterString, "application/x-www-form-urlencoded");
    std::string contentType = result->get_header_value("Content-type");
    if(result->status != 200)
    {
        log(Log::e_type::FAILURE, "POST Request to " + url + " failed with Response Code " + std::to_string(result->status));
        return(FAIL);
    }`

This is my Code. Im linking statically with -lssl -lcrypto -lhttplib -lws2_32 -lcrypt32 and CPPHTTPLIB_OPENSSL_SUPPORT is defined before #include Thanks for any Help in Advance.

EDIT: Dont know why my Code isnt displayed correctly... sorry for that

leonuetzel commented 2 years ago

Ok, i got the Problem: When using SSL (= HTTPS Request and not HTTP), then the URL doesnt have to contain the "https://", it just needs (in my case) the "etsy.api.com"