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