microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
7.96k stars 1.65k forks source link

provided uri is invalid: #1692

Closed Kelta-King closed 2 years ago

Kelta-King commented 2 years ago

I am using Instagram Graph APIs to access some instagram data. Here is one query which I'm trying to run to get the data of public instagram account.

https://graph.facebook.com/v13.0/{{business_account_id}}?fields=business_discovery.username(therock){followers_count,media_count,media{comments_count,like_count}}&access_token={{access_token}}

The above query runs just fine in Postman. But whenever I'm trying to run it using cpprest, I'm getting the following error.

provided uri is invalid: v13.0/{{business_account_id}}?fields=business_discovery.username(therock){followers_count,media_count,media{comments_count,like_count}}&access_token={{access_token}}

Here `

    utility::string_t domain = U("https://graph.facebook.com");
    utility::string_t version = U("v13.0");
    utility::string_t path = business_acc_id + U("?fields=business_discovery.username(") + username + U("){followers_count,media_count,media{comments_count,like_count}}&access_token=" + this->access_token);

    auto req = http_client(domain)

        // Making the get request
        .request(methods::GET, uri_builder(version).append_path(path).to_string())

        // Getting the response
        .then([this](http_response response){

            // If return code is not 200 then error
            if(response.status_code() != 200){
                std::cout << "Error code" << std::endl;
                throw std::runtime_error("Returned: " + std::to_string(response.status_code()));
            }

            // Returning extracted json
            return response.extract_json();

        })

        .then([this](json::value jsonObj){
            std::wcout << jsonObj.serialize() << std::endl;
            return jsonObj;
        });

        // waiting for the response
        try{
            req.wait();
        }
        catch (const std::exception& e) {
            printf("Error exception:%s\n", e.what());

        }

` I don't know what to do.

barcharcraz commented 2 years ago

It looks like that URI has some kind of string substitution that was expected to be performed but was not. cpprestsdk is reporting it's invalid because it is invalid.

Kelta-King commented 2 years ago

Thanks @barcharcraz for telling me what it means. . The issue here was it is not able to convert "{" and "}" to utf8 characters. So, for those who faces this problem, simply use the utf8 characters for these kind of characters. . . Like; "{" is equal to %7B in UTF8 encoding and "}" is equal to %7D in UTF8 encoding. So, simply use those encoded characters in your URI string. This will solve the issue.