/// This is example on usage files with MultipartForm data. /// For this example we have to create files which will be sent. import std.file; import std.path; globalLogLevel(LogLevel.info); info("Check POST files"); /// preapare files auto tmpd = tempDir(); auto tmpfname1 = tmpd ~ dirSeparator ~ "request_test1.txt"; auto f = File(tmpfname1, "wb"); f.rawWrite("file1 content\n"); f.close(); auto tmpfname2 = tmpd ~ dirSeparator ~ "request_test2.txt"; f = File(tmpfname2, "wb"); f.rawWrite("file2 content\n"); f.close(); /// /// Ok, files ready. /// Now we will prepare Form data /// File f1 = File(tmpfname1, "rb"); File f2 = File(tmpfname2, "rb"); scope(exit) { f1.close(); f2.close(); } /// /// for each part we have to set field name, source (ubyte array or opened file) and optional filename and content-type /// MultipartForm form = MultipartForm(). add(formData("Field1", cast(ubyte[])"form field from memory")). add(formData("Field2", cast(ubyte[])"file field from memory", ["filename":"data2"])). add(formData("File1", f1, ["filename":"file1", "Content-Type": "application/octet-stream"])). add(formData("File2", f2, ["filename":"file2", "Content-Type": "application/octet-stream"])); /// everything ready, send request auto rq = HTTPRequest(); auto rs = rq.post("http://httpbin.org/post", form);
This struct used to bulld POST's to forms. Each part have name and data. data is something that can be read-ed and have size. For example this can be string-like object (wrapped for reading) or opened File.