vibe.d beta banner
get vibe.d
0.10.0

Asynchronous I/O that doesn’t get in your way, written in D

Function requestHTTP

Performs a synchronous HTTP request on the specified URL.

HTTPClientResponse requestHTTP (
  string url,
  scope void delegate(scope HTTPClientRequest) requester = cast(void delegate(scope HTTPClientRequest req))null,
  const(HTTPClientSettings) settings = defaultSettings()
) @safe;

HTTPClientResponse requestHTTP (
  URL url,
  scope void delegate(scope HTTPClientRequest) requester = cast(void delegate(scope HTTPClientRequest req))null,
  const(HTTPClientSettings) settings = defaultSettings()
) @safe;

void requestHTTP (
  string url,
  scope void delegate(scope HTTPClientRequest) requester,
  scope void delegate(scope HTTPClientResponse) responder,
  const(HTTPClientSettings) settings = defaultSettings()
) @safe;

void requestHTTP (
  URL url,
  scope void delegate(scope HTTPClientRequest) requester,
  scope void delegate(scope HTTPClientResponse) responder,
  const(HTTPClientSettings) settings = defaultSettings()
) @safe;

The requester parameter allows to customize the request and to specify the request body for non-GET requests before it is sent. A response object is then returned or passed to the responder callback synchronously.

This function is a low-level HTTP client facility. It will not perform automatic redirect, caching or similar tasks. For a high-level download facility (similar to cURL), see the vibe.inet.urltransfer module.

Note that it is highly recommended to use one of the overloads that take a responder callback, as they can avoid some memory allocations and are safe against accidentally leaving stale response objects (objects whose response body wasn't fully read). For the returning overloads of the function it is recommended to put a scope(exit) right after the call in which HTTPClientResponse.dropBody is called to avoid this.

See also

download

Example

Posts a simple JSON request. Note that the server www.example.org does not exists, so there will be no meaningful result.

import vibe.core.log;
import vibe.http.client;
import vibe.stream.operations;

void test()
{
	requestHTTP("http://www.example.org/",
		(scope req) {
			req.method = HTTPMethod.POST;
			//req.writeJsonBody(["name": "My Name"]);
		},
		(scope res) {
			logInfo("Response: %s", res.bodyReader.readAllUTF8());
		}
	);
}
Authors

Sönke Ludwig, Jan Krüger

Copyright

© 2012-2014 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.