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