Search code examples
httppostvisual-studio-2019c++20

How to deal 'void cpr::Session::SetOption(const cpr::Url &)': cannot convert argument


I would like to have a function that does this (Async POST with callback):

void skip_to_next_action::operator()(const uint64_t &msg_id)
    {
        auto callback = [&](cpr::Response response)
        {
            if (m_handle_error_callback)
                m_handle_error_callback(msg_id, response);
        };

        cpr::PostAsync(cpr::Url(endpoints::player_next),
                       cpr::Bearer(get_oauth_token()),
                       cpr::Header{{s_content_type, s_application_json}},
                       callback);
    }

And when I try to compile it, I get:

error C2664: 'void cpr::Session::SetOption(const cpr::Url &)': cannot convert argument 1 from '_Ty' to 'const cpr::Url &'

message : Reason: cannot convert from '_Ty' to 'const cpr::Url'
with
[
    _Ty=spotify::skip_to_next_action::()::<lambda_1>
]

message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

message : see declaration of 'cpr::Session::SetOption'

message : see reference to function template instantiation 'void cpr::priv::set_option_internal<true,_Ty>(cpr::Session &,CurrentType &&)' being compiled
with
[
    _Ty=spotify::skip_to_next_action::()::<lambda_1>,
    CurrentType=spotify::skip_to_next_action::()::<lambda_1>
]

message : see reference to function template instantiation 'cpr::Response cpr::Post<cpr::Url,cpr::Bearer,std::map<std::string,std::string,cpr::CaseInsensitiveCompare,std::allocator<std::pair<const std::string,std::string>>>,spotify::skip_to_next_action::()::<lambda_1>>(cpr::Url &&,cpr::Bearer &&,std::map<std::string,std::string,cpr::CaseInsensitiveCompare,std::allocator<std::pair<const std::string,std::string>>> &&,spotify::skip_to_next_action::()::<lambda_1> &&)' being compiled

message : see reference to function template instantiation 'cpr::AsyncResponse cpr::PostAsync<cpr::Url,cpr::Bearer,cpr::Header,spotify::skip_to_next_action::()::<lambda_1>>(cpr::Url,cpr::Bearer,cpr::Header,spotify::skip_to_next_action::()::<lambda_1>)' being compiled

According to the docs everything should be fine. What I did wrong?

I tried to find similar problem in google, but it seems to be pretty rare problem. To deal with this I already tried adding Body{} and preconverting Url. Nothing helps. Something is wrong with the way I'm passing the callback.

I also tried to add return cpr::Url("") in callback. It also does not help.

I expect to have a non-blocking async POST request with error handling in callback.


Solution

  • If you need callbacks it's needed to use "[method]Callback(...)" functions and callback must be first param of this type of functions.

    There was no such points in cpr docs. I've figured this out, while was reviewing cpr test cases on github.