HTTPRequest

Request. Configurable parameters: headers - add any additional headers you'd like to send. authenticator - class to send auth headers. keepAlive - set true for keepAlive requests. default false. maxRedirects - maximum number of redirects. default 10. maxHeadersLength - maximum length of server response headers. default = 32KB. maxContentLength - maximun content length. delault = 5MB. bufferSize - send and receive buffer size. default = 16KB. verbosity - level of verbosity(0 - nothing, 1 - headers, 2 - headers and body progress). default = 0. proxy - set proxy url if needed. default - null.

Constructors

this
this(string uri)
Undocumented in source.

Destructor

~this
~this()
Undocumented in source.

Members

Functions

addHeaders
void addHeaders(string[string] headers)

Add headers to request

exec
HTTPResponse exec(string url, string[string] rqData)

execute POST request. Send form-urlencoded data

exec
HTTPResponse exec(string url, PostFile[] files)

send file(s) using POST

exec
HTTPResponse exec(string url, R content, string contentType)

POST data from some string(with Content-Length), or from range of strings (use Transfer-Encoding: chunked)

exec
HTTPResponse exec(string url, string[string] params)

Send request without data Request parameters will be encoded into request string

get
HTTPResponse get(A args)

GET request. Simple wrapper over exec!"GET"

post
HTTPResponse post(string uri, A args)

POST request. Simple wrapper over exec!"POST"

removeHeaders
void removeHeaders(string[] headers)

Remove headers from request

Properties

uri
URI uri [@property setter]
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

1     import std.json;
2     globalLogLevel(LogLevel.info);
3     tracef("http tests - start");
4 
5     auto rq = HTTPRequest();
6     auto rs = rq.get("https://httpbin.org/");
7     assert(rs.code==200);
8     assert(rs.responseBody.length > 0);
9     rs = HTTPRequest().get("http://httpbin.org/get", ["c":" d", "a":"b"]);
10     assert(rs.code == 200);
11     auto json = parseJSON(rs.responseBody.data).object["args"].object;
12     assert(json["c"].str == " d");
13     assert(json["a"].str == "b");
14 
15     globalLogLevel(LogLevel.info);
16     rq = HTTPRequest();
17     rq.keepAlive = true;
18     // handmade json
19     info("Check POST json");
20     rs = rq.post("http://httpbin.org/post?b=x", `{"a":"☺ ", "c":[1,2,3]}`, "application/json");
21     assert(rs.code==200);
22     json = parseJSON(rs.responseBody.data).object["args"].object;
23     assert(json["b"].str == "x");
24     json = parseJSON(rs.responseBody.data).object["json"].object;
25     assert(json["a"].str == "☺ ");
26     assert(json["c"].array.map!(a=>a.integer).array == [1,2,3]);
27     {
28         import std.file;
29         import std.path;
30         auto tmpd = tempDir();
31         auto tmpfname = tmpd ~ dirSeparator ~ "request_test.txt";
32         auto f = File(tmpfname, "wb");
33         f.rawWrite("abcdefgh\n12345678\n");
34         f.close();
35         // files
36         globalLogLevel(LogLevel.info);
37         info("Check POST files");
38         PostFile[] files = [
39                         {fileName: tmpfname, fieldName:"abc", contentType:"application/octet-stream"}, 
40                         {fileName: tmpfname}
41                     ];
42         rs = rq.post("http://httpbin.org/post", files);
43         assert(rs.code==200);
44         info("Check POST chunked from file.byChunk");
45         f = File(tmpfname, "rb");
46         rs = rq.post("http://httpbin.org/post", f.byChunk(3), "application/octet-stream");
47         assert(rs.code==200);
48         auto data = parseJSON(rs.responseBody.data).object["data"].str;
49         assert(data=="abcdefgh\n12345678\n");
50         f.close();
51     }
52     {
53         // string
54         info("Check POST utf8 string");
55         rs = rq.post("http://httpbin.org/post", "привiт, свiт!", "application/octet-stream");
56         assert(rs.code==200);
57         auto data = parseJSON(rs.responseBody.data).object["data"].str;
58         assert(data=="привiт, свiт!");
59     }
60     // ranges
61     {
62         info("Check POST chunked from lineSplitter");
63         auto s = lineSplitter("one,\ntwo,\nthree.");
64         rs = rq.exec!"POST"("http://httpbin.org/post", s, "application/octet-stream");
65         assert(rs.code==200);
66         auto data = parseJSON(rs.responseBody.toString).object["data"].str;
67         assert(data=="one,two,three.");
68     }
69     {
70         info("Check POST chunked from array");
71         auto s = ["one,", "two,", "three."];
72         rs = rq.post("http://httpbin.org/post", s, "application/octet-stream");
73         assert(rs.code==200);
74         auto data = parseJSON(rs.responseBody.data).object["data"].str;
75         assert(data=="one,two,three.");
76     }
77     {
78         info("Check POST chunked using std.range.chunks()");
79         auto s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
80         rs = rq.post("http://httpbin.org/post", s.representation.chunks(10), "application/octet-stream");
81         assert(rs.code==200);
82         auto data = parseJSON(rs.responseBody.data).object["data"].str;
83         assert(data==s);
84     }
85     // associative array
86     rs = rq.post("http://httpbin.org/post", ["a":"b ", "c":"d"]);
87     assert(rs.code==200);
88     auto form = parseJSON(rs.responseBody.data).object["form"].object;
89     assert(form["a"].str == "b ");
90     assert(form["c"].str == "d");
91     info("Check HEAD");
92     rs = rq.exec!"HEAD"("http://httpbin.org/");
93     assert(rs.code==200);
94     info("Check DELETE");
95     rs = rq.exec!"DELETE"("http://httpbin.org/delete");
96     assert(rs.code==200);
97     info("Check PUT");
98     rs = rq.exec!"PUT"("http://httpbin.org/put",  `{"a":"b", "c":[1,2,3]}`, "application/json");
99     assert(rs.code==200);
100     info("Check PATCH");
101     rs = rq.exec!"PATCH"("http://httpbin.org/patch", "привiт, свiт!", "application/octet-stream");
102     assert(rs.code==200);
103 
104     info("Check compressed content");
105     globalLogLevel(LogLevel.info);
106     rq = HTTPRequest();
107     rq.keepAlive = true;
108     rs = rq.get("http://httpbin.org/gzip");
109     assert(rs.code==200);
110     info("gzip - ok");
111     rs = rq.get("http://httpbin.org/deflate");
112     assert(rs.code==200);
113     info("deflate - ok");
114 
115     info("Check redirects");
116     globalLogLevel(LogLevel.info);
117     rq = HTTPRequest();
118     rq.keepAlive = true;
119     rs = rq.get("http://httpbin.org/relative-redirect/2");
120     assert(rs.history.length == 2);
121     assert(rs.code==200);
122 //    rq = Request();
123 //    rq.keepAlive = true;
124 //    rq.proxy = "http://localhost:8888/";
125     rs = rq.get("http://httpbin.org/absolute-redirect/2");
126     assert(rs.history.length == 2);
127     assert(rs.code==200);
128 //    rq = Request();
129     rq.maxRedirects = 2;
130     rq.keepAlive = false;
131     rs = rq.get("https://httpbin.org/absolute-redirect/3");
132     assert(rs.history.length == 2);
133     assert(rs.code==302);
134 
135     info("Check utf8 content");
136     globalLogLevel(LogLevel.info);
137     rq = HTTPRequest();
138     rs = rq.get("http://httpbin.org/encoding/utf8");
139     assert(rs.code==200);
140 
141     info("Check chunked content");
142     globalLogLevel(LogLevel.info);
143     rq = HTTPRequest();
144     rq.keepAlive = true;
145     rq.bufferSize = 16*1024;
146     rs = rq.get("http://httpbin.org/range/1024");
147     assert(rs.code==200);
148     assert(rs.responseBody.length==1024);
149 
150     info("Check basic auth");
151     globalLogLevel(LogLevel.info);
152     rq = HTTPRequest();
153     rq.authenticator = new BasicAuthentication("user", "passwd");
154     rs = rq.get("http://httpbin.org/basic-auth/user/passwd");
155     assert(rs.code==200);
156  
157     globalLogLevel(LogLevel.info);
158     info("Check exception handling, error messages are OK");
159     rq = HTTPRequest();
160     rq.timeout = 1.seconds;
161     assertThrown!TimeoutException(rq.get("http://httpbin.org/delay/3"));
162     assertThrown!ConnectError(rq.get("http://0.0.0.0:65000/"));
163     assertThrown!ConnectError(rq.get("http://1.1.1.1/"));
164     //assertThrown!ConnectError(rq.get("http://gkhgkhgkjhgjhgfjhgfjhgf/"));
165 
166     globalLogLevel(LogLevel.info);
167     info("Check limits");
168     rq = HTTPRequest();
169     rq.maxContentLength = 1;
170     assertThrown!RequestException(rq.get("http://httpbin.org/"));
171     rq = HTTPRequest();
172     rq.maxHeadersLength = 1;
173     assertThrown!RequestException(rq.get("http://httpbin.org/"));
174     tracef("http tests - ok");

Meta