requests 0.1.0

http requests


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

dlang-requests

Build Status

HTTP requests library with goals:

  • small memory footprint
  • performance
  • simple, high level API

API docs: Wiki

In simplest scenario you just need to fetch document from remote site. In this case you can call getContent

    auto r = getContent("https://httpbin.org/stream/20");
    assert(r.splitter('\n').filter!("a.length>0").count == 20);

getContent returns Buffer, filled with data. Buffer looks like Appender!ubyte (it have method data()), but also support Range operations.

When you need access to response code, you have to use Request struct for interface:

    auto rq = Request();
    auto rs = rq.get("https://httpbin.org/");
    assert(rs.code==200);

For anything other than default, you can configure Request structure for keep-alive, compressed requests, for different io buffer and maximum sizes of response headers and body.

For example to authorize with Basic authorization use next code:

    rq = Request();
    rq.authenticator = new BasicAuthentication("user", "passwd");
    rs = rq.get("http://httpbin.org/basic-auth/user/passwd");

Here is short descrition of some Request options:

nametypemeaningdefault
keepAliveboolrequest keepalive connectionfalse
maxRedirectsuintmaximum redirect depth10
maxHeadersLengthsize_tmax.acceptable response headers length32KB
maxContentLengthsize_tmax.acceptable content length5MB
timeoutDurationtimeout on connect or data transfer30.seconds
bufferSizesize_tsocket io buffer size16KB
verbosityuintverbosity level (0, 1 or 2)0
proxystringurl of the http proxynull
headersstring[string]additional headersnull

Usage example:

 auto rq = Request();
 auto rs = rq.get("http://httpbin.org/");
 writeln(rs.responseBody.data!string);

 rq.keepAlive = true;
 rs = rq.post("http://httpbin.org/post", `{"a":"b", "c":[1,2,3]}`, "application/json");
 assert(rs.code==200);

 auto f = File("tests/test.txt", "rb");
 rs = rq.post("http://httpbin.org/post", f.byChunk(3), "application/octet-stream");
 assert(rs.code==200);
 auto data = parseJSON(rs.responseBody.data).object["data"].str;
 assert(data=="abcdefgh\n12345678\n");
 f.close();

Authors:
  • ikod
Dependencies:
none
Versions:
2.1.3 2024-Jan-25
2.1.2 2023-Dec-21
2.1.1 2023-Jun-18
2.1.0 2023-Jun-16
2.0.9 2022-Oct-22
Show all 87 versions
Download Stats:
  • 48 downloads today

  • 264 downloads this week

  • 1292 downloads this month

  • 267202 downloads total

Score:
4.8
Short URL:
requests.dub.pm