expwez
expwez

Reputation: 103

HTTP not socket web requests - 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);

Upvotes: 7

Views: 163

Answers (3)

zoujiaqing
zoujiaqing

Reputation: 1

You can using hunt-http library to request every website ::

example for get:

import hunt.http;

import std.stdio;

void main()
{
    auto client = new HttpClient();

    auto request = new RequestBuilder().url("https://www.google.com").build();
    auto response = client.newCall(request).execute();

    if (response !is null)
    {
        writeln(response.getBody().asString());
    }
}

Upvotes: 0

DejanLekic
DejanLekic

Reputation: 19797

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 .

Upvotes: 1

sigod
sigod

Reputation: 6486

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]);

Upvotes: 7

Related Questions