"libs": "curl"
to your dub.json file if you are using DUB.
Windows x86 note: A DMD compatible libcurl static library can be downloaded from the dlang.org download archive page.
This module is not available for iOS, tvOS or watchOS.
Compared to using libcurl directly, this module allows simpler client code for common uses, requires no unsafe operations, and integrates better with the rest of the language. Furthermore it provides range access to protocols supported by libcurl both synchronously and asynchronously.
A high level and a low level API are available. The high level API is built entirely on top of the low level one.
The high level API is for commonly used functionality such as HTTP/FTP get. The byLineAsync and byChunkAsync functions asynchronously perform the request given, outputting the fetched content into a range.
The low level API allows for streaming, setting request headers and cookies, and other advanced features.
| Function Name | Description |
|---|---|
| High level | |
| download | download("ftp.digitalmars.com/sieve.ds", "/tmp/downloaded-ftp-file")
downloads file from URL to file system. |
| upload | upload("/tmp/downloaded-ftp-file", "ftp.digitalmars.com/sieve.ds");
uploads file from file system to URL. |
| get | get("dlang.org") returns a char[] containing the dlang.org web page. |
| put | put("dlang.org", "Hi") returns a char[] containing
the dlang.org web page. after a HTTP PUT of "hi" |
| post | post("dlang.org", "Hi") returns a char[] containing
the dlang.org web page. after a HTTP POST of "hi" |
| byLine | byLine("dlang.org") returns a range of char[] containing the
dlang.org web page. |
| byChunk | byChunk("dlang.org", 10) returns a range of ubyte10 containing the
dlang.org web page. |
| byLineAsync | byLineAsync("dlang.org") asynchronously returns a range of char[] containing the dlang.org web
page. |
| byChunkAsync | byChunkAsync("dlang.org", 10) asynchronously returns a range of ubyte10 containing the
dlang.org web page. |
| Low level | |
| HTTP | Struct for advanced HTTP usage |
| FTP | Struct for advanced FTP usage |
| SMTP | Struct for advanced SMTP usage |
Example:
import std.net.curl, std.stdio;
// Return a char[] containing the content specified by a URL
auto content = get("dlang.org");
// Post data and return a char[] containing the content specified by a URL
auto content = post("mydomain.com/here.cgi", ["name1" : "value1", "name2" : "value2"]);
// Get content of file from ftp server
auto content = get("ftp.digitalmars.com/sieve.ds");
// Post and print out content line by line. The request is done in another thread.
foreach (line; byLineAsync("dlang.org", "Post data"))
writeln(line);
// Get using a line range and proxy settings
auto client = HTTP();
client.proxy = "1.2.3.4";
foreach (line; byLine("dlang.org", client))
writeln(line);For more control than the high level functions provide, use the low level API:
Example:
import std.net.curl, std.stdio;
// GET with custom data receivers
auto http = HTTP("dlang.org");
http.onReceiveHeader =
(in char[] key, in char[] value) { writeln(key, ": ", value); };
http.onReceive = (ubyte[] data) { /+ drop +/ return data.length; };
http.perform();First, an instance of the reference-counted HTTP struct is created. Then the custom delegates are set. These will be called whenever the HTTP instance receives a header and a data buffer, respectively. In this simple example, the headers are written to stdout and the data is ignored. If the request is stopped before it has finished then return something less than data.length from the onReceive callback. See onReceiveHeader/onReceive for more information. Finally, the HTTP request is performed by calling perform(), which is synchronous.
Source: std/net/curl.d