How can I make GET and POST HTTP requests? I found Socket solution, but is it all?
string host = "google.com";
ushort port = 80;
Socket listener = new TcpSocket;
assert(listener.isAlive);
listener.blocking = false;
listener.connect(new InternetAddress(host, port));
char[] msg;
char[] req = cast(char[]) "GET /search.php HTTP/1.1\r\nHost: google.com\r\n\r\n";
listener.send(req);
Take a look at std.net.curl
. It has get
and post
methods:
import std.net.curl;
auto content = get("d-lang.appspot.com/testUrl2");
// --
auto content = post("d-lang.appspot.com/testUrl2", [1,2,3,4]);
curl is undeniably good solution. However, that would add a new dependency to your project, right? Depending on the kind of project you work on, I advise you to use Adam Ruppe's arsd
modules, in particular the http
module (he is also working on http2 one as well) - https://github.com/adamdruppe/arsd/blob/master/http.d . Or, perhaps if you want a framework, then vibe.d is your best option (http://vibed.org) as it has a HTTP client too. There are two vibe.d related, web development oriented, books that I recommend, and they are listed on the following page: http://vibed.org/tutorials.
UPDATE: There is a fantastic port of Python's requests package you should give a try: https://github.com/ikod/dlang-requests .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With