requests.request
This module provides API using Request structure.
Discussion
Structure Request provides configuration, connection pooling, cookie
persistance. You can consider it as 'Session' and reuse it - all caches and settings will effective
for next requests.
Some most usefull settings:
name | type | meaning | default |
---|---|---|---|
keepAlive | bool |
use keepalive connection | true |
verbosity | uint |
verbosity level (0, 1, 2 or 3) | 16KB |
maxRedirects | uint |
maximum redirects allowed (0 to disable redirects) | true |
maxHeadersLength | size_t |
max. acceptable response headers length | 32KB |
maxContentLength | size_t |
max. acceptable content length | 0 - unlimited |
bufferSize | size_t |
socket io buffer size | 16KB |
timeout | Duration |
timeout on connect or data transfer | 30.seconds |
proxy | string |
url of the HTTP/HTTPS/FTP proxy | null |
bind | string |
use local address for outgoing connections | null |
useStreaming | bool |
receive response data as InputRange in case it can't fit memory |
false |
addHeaders | string[string] |
custom headers | null |
cookie | Cookie[] |
cookies you will send to server | null |
authenticator | Auth |
authenticator | null |
socketFactory | see doc for socketFactory | user-provided connection factory | null |
Example:
import requests;
import std.datetime;
void main()
{
Request rq = Request();
Response rs;
rq.timeout = 10.seconds;
rq.addHeaders(["User-Agent": "unknown"]);
rs = rq.get("https://httpbin.org/get");
assert(rs.code==200);
rs = rq.post("http://httpbin.org/post", "abc");
assert(rs.code==200);
}
-
Declaration
interface
Interceptor
;'Intercepror' intercepts Request. It can modify request, or log it, or cache it or do whatever you need. When done it can return response or pass it to next request handler.
Example:
class ChangePath : Interceptor { Response opCall(Request r, RequestHandler next) { r.path = r.path ~ "get"; auto rs = next.handle(r); return rs; } }
Example:
Request rq; rq.addInterceptor(new ChangePath()); rq.get("http://example.com");
-
Declaration
void
addInterceptor
(Interceptori
);Add module-level interceptor. Each Request will include it in the processing. it is handy as you can change behaviour of your requests without any changes in your code, just install interceptor before any call to Request().
-
Declaration
struct
Request
;Structure
Request
provides configuration, connection pooling, cookie persistance. You can consider it as 'Session'.-
Declaration
pure @property @safe void
uri
(stringu
);Set and Get
uri
for next request. -
Declaration
pure @nogc @property void
path
(stringp
);
const pure @nogc @property @safe stringpath
();Set/Get
path
for next request. -
Declaration
pure nothrow @nogc @property @safe void
sslSetVerifyPeer
(boolv
);Enable/disable ssl peer verification..
-
Declaration
pure nothrow @nogc @property @safe void
sslSetKeyFile
(stringp
, SSLOptions.filetypet
= SSLOptions.filetype.pem);Set path and format for ssl key file.
-
Declaration
pure nothrow @nogc @property @safe void
sslSetCertFile
(stringp
, SSLOptions.filetypet
= SSLOptions.filetype.pem);Set path and format for ssl certificate file.
-
Declaration
pure nothrow @nogc @property @safe void
sslSetCaCert
(stringpath
);Set
path
to certificate authority file. -
Declaration
void
addHeaders
(in string[string]headers
);Add
headers
to requestParameters
string[string]
headers
headers
to send.Example:
rq = Request(); rq.keepAlive = true; rq.addHeaders(["X-Header": "test"]);
-
Declaration
void
clearHeaders
();Remove any previously added headers.
-
Declaration
Response
exec
(string method = "GET", A...)(stringurl
, Aargs
);Execute GET for http and retrieve file for FTP. You have to provide at least uri. All other arguments should conform to HTTPRequest.get or FTPRequest.get depending on the URI scheme. When arguments do not conform scheme (for example you try to call get("ftp://somehost.net/pub/README", {"a":"b"}) which doesn't make sense) you will receive Exception("Operation not supported for ftp")
Discussion
Execute POST for http and STOR file for FTP. You have to provide uri and data. Data should conform to HTTPRequest.post or FTPRequest.post depending on the URI scheme. When arguments do not conform scheme you will receive Exception("Operation not supported for ftp")
-
Declaration
const @property bool
hasMultipartForm
();helper
-
Declaration
void
addInterceptor
(Interceptori
);Add interceptor to request.
-