http_req( but need the http code on failure

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
taylorDH0210
Posts: 5
Joined: Thu Oct 28, 2021 9:24 pm

http_req( but need the http code on failure

Post by taylorDH0210 »

This code is working fine. My only problem is when it fails, I cannot capture the HTTP status code, 401 for example.
The gReponse variable contains the text for the reason " {"message":"Invalid username/password."} ". I could force the errors and translate text to error codes, but I would rather not.

Code: Select all

gResponseCode = http_req(
                'POST'
               : url
               : *OMIT       // File to receive
               : gResponse   // String to receive
               : gZipPath    // File to send
               : *OMIT       // gRequest
               : 'application/json' );

And, yes, I have found this so much better than reinventing the wheel. Merci beaucoup, Scott.
Scott Klement
Site Admin
Posts: 667
Joined: Sun Jul 04, 2021 5:12 am

Re: http_req( but need the http code on failure

Post by Scott Klement »

The 401 should be in gResponseCode in your example. Is it not?
taylorDH0210
Posts: 5
Joined: Thu Oct 28, 2021 9:24 pm

Re: http_req( but need the http code on failure

Post by taylorDH0210 »

Well, I did see that with another code, 404, but the 401 came out as -1. So did the timeout just now. I log the code and the message text.

404 | {"Message":"No HTTP resource was found that matches the request URI
-1 | {"message":"Invalid username/password."}
200 | {"StatusCode":200,"Message":null,"ProcessId":41697642,"BatchId":413342}
Scott Klement
Site Admin
Posts: 667
Joined: Sun Jul 04, 2021 5:12 am

Re: http_req( but need the http code on failure

Post by Scott Klement »

Oh, I bet 401 is expecting you to provide auth codes.

Try calling http_error and passing the 2nd optional parameter.

Code: Select all

dcl-s status int(10);
.
...  code for http_req goes here ...
.
http_error(*omit: status);
Scott Klement
Site Admin
Posts: 667
Joined: Sun Jul 04, 2021 5:12 am

Re: http_req( but need the http code on failure

Post by Scott Klement »

You could also look for the HTTP_NDAUTH error code, if 401 means it needs authentication.

Code: Select all

dcl-s err int(10);
.
.
... code to call http_req here ...
.
.
if rc = -1;
   http_error(err);
   if err = HTTP_NDAUTH;
      // http server asked for authentication.  Use http_setAuth() to provide it.
   endif;
endif;
taylorDH0210
Posts: 5
Joined: Thu Oct 28, 2021 9:24 pm

Re: http_req( but need the http code on failure

Post by taylorDH0210 »

Will try today. Thanks for the quick response.
taylorDH0210
Posts: 5
Joined: Thu Oct 28, 2021 9:24 pm

Re: http_req( but need the http code on failure

Post by taylorDH0210 »

This worked great!

http_error(*omit: status);
Post Reply