How to get to body of HTTP 500 error with std.net.curl.get()?

Boris Carvajal boris2.9 at gmail.com
Sat Feb 15 21:37:15 UTC 2020


On Friday, 14 February 2020 at 00:24:27 UTC, Gregor Mückl wrote:
> Hi!
>
> I am trying to write a client for pretty... well... creatively 
> designed web API. The server gives HTTP status 500 replies if 
> the requests are malformed, but the actual error message is 
> hidden in the body of the reply (an XML document!). 
> std.net.curl.get() throws an exception in this case. But I 
> would like to get the body to process the error message within. 
> How can I do that?
>
> Thanks in advance,
> Gregor

It seems you can't, that simple get wrapper is very limited.
Better create you own get function, example:

import std;

ushort get(string url, ref ubyte[] content)
{
     auto cont = appender(&content);
     auto http = HTTP();
     http.method = HTTP.Method.get;
     http.url = url;
     http.onReceive = (ubyte[] data)
     {
         cont ~= data;
         return data.length;
     };
     HTTP.StatusLine status;
     http.onReceiveStatusLine = (HTTP.StatusLine s) { status = s; 
};
     http.perform();
     return status.code;
}

void main()
{
     ubyte[] data;
     auto scode = get("http://dlang.org/asdf", data);
     if (scode == 404)
     {
     // data has the body
     ...
}


More information about the Digitalmars-d-learn mailing list