3

how can I retrieve the response cookies from a curlpp request?

I want to store the PHP session off of a HTTP GET request. This is my current code:

void Grooveshark::Connection::processPHPCookie()
{
    std::ostringstream buffer;

    gsDebug("Processing PHP cookie...");

    try {
        request.setOpt<cURLpp::Options::Url>("http://listen.grooveshark.com");
        request.setOpt<cURLpp::Options::WriteStream>(&buffer);
        request.perform();

        // Get the PHP Session cookie here..

    } catch (cURLpp::LogicError& exception) {
        gsError(exception.what());
    } catch (cURLpp::RuntimeError& exception) {
        gsError(exception.what());
    }

    gsDebug("Processing complete...");
}

request is a cURLpp::Easy instance. If you need more details you can find my source code here

Thanks in advance.

mkroman
  • 421
  • 1
  • 5
  • 13

3 Answers3

1

First, set exEasy.setOpt(curlpp::options::CookieFile("") then call exEasy.perform(), then loop through

std::list<std::string> cookies;
curlpp::infos::CookieList::get(exEasy, cookies);
Dmitry T
  • 11
  • 1
0

https://bitbucket.org/moriarty/curlpp/src/ac658073c87a/examples/example07.cpp

That example seems to have what you want. In particular this code:

std::cout << "\nCookies from cookie engine:" << std::endl;
std::list<std::string> cookies;
curlpp::infos::CookieList::get(exEasy, cookies);
int i = 1;
for (std::list<std::string>::const_iterator it = cookies.begin(); it != cookies.end(); ++it, i++)
{
    std::cout << "[" << i << "]: " << MakeCookie(*it) << std::endl;
}

Note that MakeCookie returns a struct called MyCookie inside the example, so you would also need:

struct MyCookie
{
        std::string name;
        std::string value;
        std::string domain;
        std::string path;
        time_t expires;
        bool tail;
        bool secure;
};

MyCookie
MakeCookie(const std::string &str_cookie)
{
        std::vector<std::string> vC = splitCookieStr(str_cookie);
        MyCookie cook;

        cook.domain = vC[0];
        cook.tail = vC[1] == "TRUE";
        cook.path = vC[2];
        cook.secure = vC[3] == "TRUE";
        cook.expires = StrToInt(vC[4]);
        cook.name = vC[5];
        cook.value = vC[6];

        return cook;
}
  • I think this is the link now: https://github.com/datacratic/curlpp/blob/master/examples/example07.cpp – jman Apr 14 '19 at 23:32
  • I've tried this right now and it's not working, despite I've put the verbose to true and it shows a "Set-Cookie" header in the response. Any ideas? – Raul Luna Mar 04 '21 at 22:11
0

The previous answers link is now located at: https://github.com/datacratic/curlpp/blob/master/examples/example07.cpp

It should be noted that if one wants to just get cookie responses then on must pass an empty string to the cookie lists.

For the previous example exEasy.setOpt(new curlpp::options::CookieList("")) would need to be added in order to get cookie strings (something other than an empty string might be used but I was not able to find further documentation).

jman
  • 685
  • 5
  • 15