digitalmars.D.announce - allow response status codes with curl
- Hampus (8/8) Oct 02 2019 I have a program that is using std.curl and right now if a http
- Anonymouse (6/14) Oct 02 2019 The HTTPStatusException class has a `.status` int member that
- Boris Carvajal (30/38) Oct 02 2019 Catch that exception then do whatever you want, as in this code:
I have a program that is using std.curl and right now if a http request returns a status code other than 200 the program will crash like this: std.net.curl.HTTPStatusException C:\D\dmd2\windows\bin\..\..\src\phobos\s d\net\curl.d(1082): HTTP request returned status code 404 (Not Found) What I want to do is allow certain stauts codes and I want my program to keep going if it receives for example 404 or 500 but raise an exception for lets say status code 403. How do I achieve this?
Oct 02 2019
On Thursday, 3 October 2019 at 01:02:43 UTC, Hampus wrote:I have a program that is using std.curl and right now if a http request returns a status code other than 200 the program will crash like this: std.net.curl.HTTPStatusException C:\D\dmd2\windows\bin\..\..\src\phobos\s d\net\curl.d(1082): HTTP request returned status code 404 (Not Found) What I want to do is allow certain stauts codes and I want my program to keep going if it receives for example 404 or 500 but raise an exception for lets say status code 403. How do I achieve this?The HTTPStatusException class has a `.status` int member that contains the returned status code. Is that what you're looking for? https://dlang.org/library/std/net/curl/http_status_exception.html Also this thread belongs in the Learn forum.
Oct 02 2019
On Thursday, 3 October 2019 at 01:02:43 UTC, Hampus wrote:I have a program that is using std.curl and right now if a http request returns a status code other than 200 the program will crash like this: std.net.curl.HTTPStatusException C:\D\dmd2\windows\bin\..\..\src\phobos\s d\net\curl.d(1082): HTTP request returned status code 404 (Not Found) What I want to do is allow certain stauts codes and I want my program to keep going if it receives for example 404 or 500 but raise an exception for lets say status code 403.First: You post in the wrong forum section.How do I achieve this?Catch that exception then do whatever you want, as in this code: import std; void main() { try { get("dlang.org/aaaaaaaaaaa"); } catch(CurlException ce) { if(auto e = cast(HTTPStatusException) ce) { if (e.status.among(403, 444)) throw new Exception("bad status, ending program."); if (e.status.among(404, 500)) writeln("request with status: ", e.status, ", but keep going."); } else if(auto e = cast(CurlTimeoutException) ce) { // TODO: implement retry } else { writeln(ce.msg); } } writeln("program finished."); }
Oct 02 2019