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

Anonymouse zorael at gmail.com
Sat Feb 15 20:38:51 UTC 2020


On Saturday, 15 February 2020 at 16:25:42 UTC, Gregor Mückl wrote:
> Unfortunately, this is not true. The msg only contains the text 
> information in the status line of the HTTP reply. If I'm not 
> mistaken, the exception is created in this line in 
> std/net/curl.d:
>
>     enforce(statusLine.code / 100 == 2, new 
> HTTPStatusException(statusLine.code,
>             format("HTTP request returned status code %d (%s)", 
> statusLine.code, statusLine.reason)));
>
> If anything is wrong, the server I'm interested in tends to 
> reply with a status code 500 and a generic status line text and 
> a bloated XML body containing the actual error message.

Not that it answers your question but requests[1] can do this, if 
you don't mind adding some dependencies.

Request req;
Response res = req.get(urlRespondingWith500);
assert(res.code == 500);
writeln(res.responseBody);  // Buffer!ubyte; use .to!string to 
get a string

When testing to confirm I ran into a bug[2] where the body is 
sometimes empty, but outside of fringe cases it should work.

[1]: https://code.dlang.org/packages/requests
[2]: https://github.com/ikod/dlang-requests/issues/115


More information about the Digitalmars-d-learn mailing list