Search code examples
httpd

How to get server response code on D?


I need to check server response code in D. For example check if server return 404, 200 or some another code.

I looked at std.net.curl, but I do not understand how to use it.

I am not sure but possible I need to use option request ex:

 import std.net.curl;
 import std.stdio;

void main()
{
    auto http = HTTP();
    options("dlang.org", null, http);
    writeln("Allow set to " ~ http.responseHeaders["Allow"]);
}

This codу do not work for me. I am getting next error:

F:\temp\1>app.exe
[email protected](8): Range violation
----------------
0x0041DB08
0x00402092
0x00426D8E
0x00426D63
0x00426C79
0x0041D857
0x7636338A in BaseThreadInitThunk
0x77C79F72 in RtlInitializeExceptionChain
0x77C79F45 in RtlInitializeExceptionChain

Solution

  • You just attach a statusLine callback:

    auto http = HTTP("dlang.org");
    http.onReceiveStatusLine = (HTTP.StatusLine status){ responceCode = status.code; };
    //attach onreceive callback as well
    http.perform();