Skip to content

Http Client

Brian Zou edited this page Mar 14, 2019 · 2 revisions

How to create http client object?

auto client = new hunt.http.client.Client();
auto res = client->request('GET', 'https://api.github.com/user', [
    'auth' => ['user', 'pass']
]);
writeln(res->getStatusCode());
// "200"
writeln(res->getHeader('content-type')[0]);
// 'application/json; charset=utf8'
writeln(res->getBody());
// {"type":"User"...'

// Send an asynchronous request.
auto request = new hunt.http.client.Request('GET', 'http://httpbin.org');
auto promise = client->sendAsync(request)->then(function (response) {
    writeln('I completed! ' ~ response->getBody());
});

promise->wait();
Clone this wiki locally