getContent

Call GET, and return response content. args = variadic args to supply parameter names and values.

  1. auto getContent(string url)
  2. auto getContent(string url, string[string] args)
  3. auto getContent(string url, QueryParam[] args)
  4. auto getContent(string url, A args)
    getContent
    (
    A...
    )
    (
    string url
    ,)
    if (
    args.length > 1 &&
    args.length % 2 == 0
    )

Return Value

Type: auto

Buffer!ubyte which you can use as ForwardRange or DirectAccessRange, or extract data with .data() method.

Examples

import std.algorithm;
import std.stdio;
import std.json;
globalLogLevel(LogLevel.info);
info("Test getContent");
auto r = getContent("https://httpbin.org/stream/20");
assert(r.splitter('\n').filter!("a.length>0").count == 20);
r = getContent("ftp://speedtest.tele2.net/1KB.zip");
assert(r.length == 1024);
r = getContent("https://httpbin.org/get", ["a":"b", "c":"d"]);

string name = "user", sex = "male";
int    age = 42;
r = getContent("https://httpbin.org/get", "name", name, "age", age, "sex", sex);

info("Test receiveAsRange with GET");
auto rq = Request();
rq.useStreaming = true;
rq.bufferSize = 16;
auto rs = rq.get("http://httpbin.org/get");
auto stream = rs.receiveAsRange();
ubyte[] streamedContent;
while( !stream.empty() ) {
    streamedContent ~= stream.front;
    stream.popFront();
}
rq = Request();
rs = rq.get("http://httpbin.org/get");
assert(streamedContent == rs.responseBody.data);
rq.useStreaming = true;
streamedContent.length = 0;
rs = rq.get("ftp://speedtest.tele2.net/1KB.zip");
stream = rs.receiveAsRange;
while( !stream.empty() ) {
    streamedContent ~= stream.front;
    stream.popFront();
}
globalLogLevel(LogLevel.info);
info("Test get in parallel");
import std.stdio;
import std.parallelism;
import std.algorithm;
import std.string;
import core.atomic;

immutable auto urls = [
    "http://httpbin.org/stream/10",
    "https://httpbin.org/stream/20",
    "http://httpbin.org/stream/30",
    "https://httpbin.org/stream/40",
    "http://httpbin.org/stream/50",
    "https://httpbin.org/stream/60",
    "http://httpbin.org/stream/70",
];

defaultPoolThreads(5);

shared short lines;

foreach(url; parallel(urls)) {
    atomicOp!"+="(lines, getContent(url).splitter("\n").count);
}
assert(lines == 287);

Meta