postContent

Call post and return response content.

postContent
(
A...
)
(
string url
,)

Examples

import std.json;
import std.string;
import std.stdio;
import std.range;

globalLogLevel(LogLevel.info);
info("Test postContent");
auto r = postContent("http://httpbin.org/post", `{"a":"b", "c":1}`, "application/json");
assert(parseJSON(r.data).object["json"].object["c"].integer == 1);

/// ftp upload from range
info("Test postContent ftp");
r = postContent("ftp://speedtest.tele2.net/upload/TEST.TXT", "test, ignore please\n".representation);
assert(r.length == 0);

/// Posting to forms (for small data)
///
/// posting query parameters using "application/x-www-form-urlencoded"
info("Test postContent using query params");
postContent("http://httpbin.org/post", queryParams("first", "a", "second", 2));

/// posting using multipart/form-data (large data and files). See docs fot HTTPRequest
info("Test postContent form");
MultipartForm form;
form.add(formData(/* field name */ "greeting", /* content */ cast(ubyte[])"hello"));
postContent("http://httpbin.org/post", form);

/// you can do this using Request struct to access response details
info("Test postContent form via Request()");
auto rq = Request();
form = MultipartForm().add(formData(/* field name */ "greeting", /* content */ cast(ubyte[])"hello"));
auto rs = rq.post("http://httpbin.org/post", form);
assert(rs.code == 200);

info("Test receiveAsRange with POST");
rq = Request();
rq.useStreaming = true;
rq.bufferSize = 16;
string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
rs = rq.post("http://httpbin.org/post", s.representation.chunks(10), "application/octet-stream");
auto stream = rs.receiveAsRange();
ubyte[] streamedContent;
while( !stream.empty() ) {
    streamedContent ~= stream.front;
    stream.popFront();
}
rq = Request();
rs = rq.post("http://httpbin.org/post", s.representation.chunks(10), "application/octet-stream");
assert(streamedContent == rs.responseBody.data);

Meta