digitalmars.D.learn - How to get to body of HTTP 500 error with std.net.curl.get()?
- Gregor =?UTF-8?B?TcO8Y2ts?= (9/9) Feb 13 2020 Hi!
- Andre Pany (6/16) Feb 14 2020 Hi Gregor,
- Gregor =?UTF-8?B?TcO8Y2ts?= (11/29) Feb 15 2020 Unfortunately, this is not true. The msg only contains the text
- Anonymouse (12/23) Feb 15 2020 Not that it answers your question but requests[1] can do this, if
- ikod (15/20) Feb 16 2020 Just a note - this is not a bug, server really send empty body:
- Anonymouse (2/16) Feb 16 2020 Yes, my bad. I was assuming curl's output was what was sent.
- Boris Carvajal (30/40) Feb 15 2020 It seems you can't, that simple get wrapper is very limited.
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
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, GregorHi 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
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: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.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, GregorHi 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 15 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
Feb 15 2020
On Saturday, 15 February 2020 at 20:38:51 UTC, Anonymouse wrote:On Saturday, 15 February 2020 at 16:25:42 UTC, Gregor MücklWhen 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/115Just 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
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 receivedYes, my bad. I was assuming curl's output was what was sent.
Feb 16 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, GregorIt 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