简体   繁体   中英

Passing multiple POST request parameters C++ libcurl

Currently using the C++ implementation of libcurl to interact with the Spotify API, looking for a way to pass multiple 'Request Body Parameters' during a POST request. The fields required are:

需要的字段

Looking at the example of a POST request found in libcurl's documentation, it seems that this line:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");

passes two parameters: "name" and "project". When I try a similar format with Spotify's API:

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=abcdef&redirect_uri=example.com");

I get the following error:

{"error":"unsupported_grant_type","error_description":"grant_type parameter is missing"}

I've validated that CURLOPT_POSTFIELDS works for this case, by passing only the "grant_type" , because the API response tells me that my request is missing a code, so clearly the API is reading the POSTFIELDS argument.

Does anyone have any insight on how to include multiple parameters in a POST request?

EDIT: Providing a minimal reproducible example: As part of the oAuth flow, this example happens AFTER the user receives a oAuth access token

CURL *curl;
std::string res;

curl = curl_easy_init();
    
if(curl) {
    try {
        curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 0);
        curl_easy_setopt(curl, CURLOPT_URL, "https://accounts.spotify.com/api/token");

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=abcdef&redirect_uri=example.com");     
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);

        std::string enc = base64_encode(reinterpret_cast<const unsigned char*>((myClientID + ":" + myClientSecret).data()), (myclientID + ":" + myClientSecret).length(), false);
            
        std::string httpAuth = "Authorization: Basic " + enc;
        struct curl_slist *authChunk = nullptr;
        authChunk = curl_slist_append(authChunk, httpAuth.c_str());

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, authChunk);

        curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    catch (const char* Exception) {
        std::cerr << Exception << std::endl;
    }
}

For anyone encountering this issue - make sure that you are encoding your redirect_uri parameter correctly when formatting your POST request. Here is what I did differently

// Wrote a custom URL encoding function

std::string urlEncEasy(std::string url) {
    std::string res;

    for (auto c : url) {
        if (c == ':') {
            res += "%3A";
        }
        else if (c == '/') {
            res += "%2F";
        }
        else {
            res += c;
        }
    } 
    return res;
}

// Changed my CURLOPT_POSTFIELDS to
std::string postFields = "grant_type=authorization_code&code=" + myAuthToken + "&redirect_uri=" + urlEncEasy(myRedirectUri);

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM