2

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
core.exception.RangeError@app.d(8): Range violation
----------------
0x0041DB08
0x00402092
0x00426D8E
0x00426D63
0x00426C79
0x0041D857
0x7636338A in BaseThreadInitThunk
0x77C79F72 in RtlInitializeExceptionChain
0x77C79F45 in RtlInitializeExceptionChain
Dmitry Bubnenkov
  • 9,415
  • 19
  • 85
  • 145

2 Answers2

2

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();
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • I am getting error "Error: undefined identifier StatusLine" what I should add to your example to get it's work, except main() function? – Suliman Dec 18 '14 at 19:52
  • I can't understand why I am getting content of all page instead of server response code: `auto http = HTTP("dlang.org"); http.onReceiveStatusLine = (HTTP.StatusLine status){ writeln(status.code); }; http.perform();` I had changed StatusLine to HTTP.StatusLine. Am I right understand that structures in D could be inner (one in another StatusLine in HTTP)? – Suliman Dec 18 '14 at 20:30
0

Working code:

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

void main()
{

auto http = HTTP("dlang.org");
http.onReceiveStatusLine = (HTTP.StatusLine status){ writeln(status.code); };
http.onReceive = (ubyte[] data) { /+ drop +/ return data.length; };
http.perform();

}

I would be very grateful, if somebody will explain me why I should to call onReceive and what data in onReceive braces means

Suliman
  • 1,469
  • 3
  • 13
  • 19
  • you attach a callback to onReceive, the parameter passed in is the chunk of data that comes in from the server (usually what you are after when getting from a server) – ratchet freak Dec 18 '14 at 23:08