HttpClient.retainCookies

HttpClient does NOT automatically store cookies. You must explicitly retain them from a response by calling this method.

class HttpClient
void
retainCookies

Bugs

It does NOT currently implement domain / path / secure separation nor cookie expiration. It assumes that if you call this function, you're ok with it.

You may want to use separate HttpClient instances if any sharing is unacceptable at this time.

Examples

import arsd.http2;
void main() {
	auto client = new HttpClient();
	auto setRequest = client.request(Uri("http://arsdnet.net/cgi-bin/cookies/set"));
	auto setResponse = setRequest.waitForCompletion();

	auto request = client.request(Uri("http://arsdnet.net/cgi-bin/cookies/get"));
	auto response = request.waitForCompletion();

	// the cookie wasn't explicitly retained, so the server echos back nothing
	assert(response.responseText.length == 0);

	// now keep the cookies from our original set
	client.retainCookies(setResponse);

	request = client.request(Uri("http://arsdnet.net/cgi-bin/cookies/get"));
	response = request.waitForCompletion();

	// now it matches
	assert(response.responseText.length && response.responseText == setResponse.cookies["example-cookie"]);
}

Meta

History

Added July 5, 2021 (dub v10.2)