requests.http

  • Declaration

    class HTTPResponse: requests.base.Response;

    Response - result of request execution.

    Discussion

    Response.code - response HTTP code. Response.status_line - received HTTP status line. Response.responseHeaders - received headers. Response.responseBody - container for received body Response.history - for redirected responses contain all history

  • Declaration

    struct HTTPRequest;

    Request. Configurable parameters: method - string, method to use (GET, POST, ...) headers - string[string], add any additional headers you'd like to send. authenticator - class Auth, class to send auth headers. keepAlive - bool, set true for keepAlive requests. default true. maxRedirects - uint, maximum number of redirects. default 10. maxHeadersLength - size_t, maximum length of server response headers. default = 32KB. maxContentLength - size_t, maximun content length. delault - 0 = unlimited. bufferSize - size_t, send and receive buffer size. default = 16KB. verbosity - uint, level of verbosity(0 - nothing, 1 - headers, 2 - headers and body progress). default = 0. proxy - string, set proxy url if needed. default - null. cookie - Tuple Cookie, Read/Write cookie You can get cookie setted by server, or set cookies before doing request. timeout - Duration, Set timeout value for connect/receive/send.

    • Declaration

      void addHeaders(in string[string] headers);

      Add headers to request

      Parameters

      string[string] headers

      headers to send.

    • Declaration

      pure void removeHeaders(in string[] headers);

      Remove headers from request

      Parameters

      string[] headers

      headers to remove.

    • Declaration

      void close_connection_if_not_keepalive(NetworkStream _stream);

      If we do not want keepalive request, or server signalled to close connection, then close it

    • Declaration

      HTTPResponse exec(string method = "POST")(string url, MultipartForm sources);

      Send multipart for request. You would like to use this method for sending large portions of mixed data or uploading files to forms. Content of the posted form consist of sources. Each source have at least name and value (can be string-like object or opened file, see more docs for MultipartForm struct)

      Parameters

      string url

      url

      MultipartForm sources

      array of sources.

    • Declaration

      HTTPResponse exec(string method = "POST", R)(string url, R content, string contentType = "application/octet-stream") if (rank!R == 1 || rank!R == 2 && isSomeChar!(Unqual!(typeof(content.front.front))) || rank!R == 2 && is(Unqual!(typeof(content.front.front)) == ubyte));

      POST/PUT/... data from some string(with Content-Length), or from range of strings/bytes (use Transfer-Encoding: chunked). When rank 1 (flat array) used as content it must have length. In that case "content" will be sent directly to network, and Content-Length headers will be added. If you are goung to send some range and do not know length at the moment when you start to send request, then you can send chunks of chars or ubyte. Try not to send too short chunks as this will put additional load on client and server. Chunks of length 2048 or 4096 are ok.

      Parameters: url = url content = string or input range contentType = content type

      Return Value

      Response

      Examples

      1. rs = rq.exec!"POST"("http://httpbin.org/post", "привiт, свiт!", "application/octet-stream"); auto s = lineSplitter("one,\ntwo,\nthree."); rs = rq.exec!"POST"("http://httpbin.org/post", s, "application/octet-stream"); auto s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; rs = rq.exec!"POST"("http://httpbin.org/post", s.representation.chunks(10), "application/octet-stream"); auto f = File("tests/test.txt", "rb"); rs = rq.exec!"POST"("http://httpbin.org/post", f.byChunk(3), "application/octet-stream");

    • Declaration

      HTTPResponse exec(string method = "GET")(string url = null, QueryParam[] params = null);

      Send request with parameters. If used for POST or PUT requests then application/x-www-form-urlencoded used. Request parameters will be encoded into request string or placed in request body for POST/PUT requests.

      Parameters: url = url params = request parameters

      Return Value

      Response

      Examples

      1. rs = Request().exec!"GET"("http://httpbin.org/get", ["c":"d", "a":"b"]);

    • Declaration

      HTTPResponse exec(string method = "POST")(string url, PostFile[] files) if (method == "POST");

      WRAPPERS

      Discussion

      send file(s) using POST and multipart form. This wrapper will be deprecated, use post with MultipartForm - it is more general and clear.

      Parameters: url = url files = array of PostFile structures

      Return Value

      Response Each PostFile structure contain path to file, and optional field name and content type. If no field name provided, then basename of the file will be used. application/octet-stream is default when no content type provided.

      Example:

      1. PostFile[] files = [ {fileName:"tests/abc.txt", fieldName:"abc", contentType:"application/octet-stream"}, {fileName:"tests/test.txt"} ]; rs = rq.exec!"POST"("http://httpbin.org/post", files);

    • Declaration

      HTTPResponse exec(string method = "GET")(string url, string[string] params);

      exec request with parameters when you can use dictionary (when you have no duplicates in parameter names) Consider switch to exec(url, QueryParams) as it more generic and clear.

      Parameters: url = url params = dictionary with field names as keys and field values as values.

      Return Value

      Response

    • get

      Declaration

      HTTPResponse get(A...)(A args);

      GET request. Simple wrapper over exec!"GET"

      Parameters

      A args

      request parameters. see exec docs.

    • Declaration

      HTTPResponse post(A...)(string uri, A args);

      POST request. Simple wrapper over exec!"POST"

      Parameters

      string uri

      endpoint uri

      A args

      request parameters. see exec docs.