Function requestHTTP

Performs a HTTP request on the specified URL.

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.

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 accidentially 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.

Prototypes

HTTPClientResponse requestHTTP(
  string url,
  scope void delegate(scope HTTPClientRequest) requester
);

HTTPClientResponse requestHTTP(
  URL url,
  scope void delegate(scope HTTPClientRequest) requester
);

void requestHTTP(
  string url,
  scope void delegate(scope HTTPClientRequest) requester,
  scope void delegate(scope HTTPClientResponse) responder
);

void requestHTTP(
  URL url,
  scope void delegate(scope HTTPClientRequest) requester,
  scope void delegate(scope HTTPClientResponse) responder
);

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 RejectedSoftware e.K.

License

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