Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP not socket web requests - D

Tags:

d

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);
like image 341
expwez Avatar asked Apr 07 '16 15:04

expwez


2 Answers

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]);
like image 141
sigod Avatar answered Oct 21 '22 23:10

sigod


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 .

like image 43
DejanLekic Avatar answered Oct 22 '22 00:10

DejanLekic