www.digitalmars.com         C & C++   DMDScript  

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

reply Gregor =?UTF-8?B?TcO8Y2ts?= <gregormueckl gmx.de> writes:
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
Feb 13 2020
next sibling parent reply Andre Pany <andre s-e-a-p.de> writes:
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
Hi Gregor, If I am not completely wrong, the exception has an attribute "msg" which should contain the body of the http response. Kind regards André
Feb 14 2020
parent reply Gregor =?UTF-8?B?TcO8Y2ts?= <gregormueckl gmx.de> writes:
On Friday, 14 February 2020 at 13:23:01 UTC, Andre Pany wrote:
 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
Hi Gregor, If I am not completely wrong, the exception has an attribute "msg" which should contain the body of the http response. Kind regards André
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.
Feb 15 2020
parent reply Anonymouse <zorael gmail.com> writes:
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
Feb 15 2020
parent reply ikod <geller.garry gmail.com> writes:
On Saturday, 15 February 2020 at 20:38:51 UTC, Anonymouse wrote:
 On Saturday, 15 February 2020 at 16:25:42 UTC, Gregor Mückl
 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
Just a note - this is not a bug, server really send empty body: < HTTP/1.1 500 Internal Server Error < cache-control: private < server: Microsoft-IIS/10.0 < x-aspnetmvc-version: 5.1 < access-control-allow-origin: * < x-aspnet-version: 4.0.30319 < request-context: appId=cid-v1:7585021b-2db7-4da6-abff-2cf23005f0a9 < access-control-expose-headers: Request-Context < x-powered-by: ASP.NET < date: Sat, 15 Feb 2020 20:44:03 GMT < content-length: 0 < 0 bytes of body received
Feb 16 2020
parent Anonymouse <zorael gmail.com> writes:
On Sunday, 16 February 2020 at 09:13:54 UTC, ikod wrote:
 Just a note - this is not a bug, server really send empty body:

 < HTTP/1.1 500 Internal Server Error
 < cache-control: private
 < server: Microsoft-IIS/10.0
 < x-aspnetmvc-version: 5.1
 < access-control-allow-origin: *
 < x-aspnet-version: 4.0.30319
 < request-context: 
 appId=cid-v1:7585021b-2db7-4da6-abff-2cf23005f0a9
 < access-control-expose-headers: Request-Context
 < x-powered-by: ASP.NET
 < date: Sat, 15 Feb 2020 20:44:03 GMT
 < content-length: 0
 < 0 bytes of body received
Yes, my bad. I was assuming curl's output was what was sent.
Feb 16 2020
prev sibling parent Boris Carvajal <boris2.9 gmail.com> writes:
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 ... }
Feb 15 2020