Page 1 of 1

http_req( but need the http code on failure

Posted: Thu Oct 28, 2021 9:29 pm
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.

Re: http_req( but need the http code on failure

Posted: Fri Oct 29, 2021 3:17 pm
by Scott Klement
The 401 should be in gResponseCode in your example. Is it not?

Re: http_req( but need the http code on failure

Posted: Fri Oct 29, 2021 7:07 pm
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}

Re: http_req( but need the http code on failure

Posted: Sat Oct 30, 2021 4:35 am
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);

Re: http_req( but need the http code on failure

Posted: Sat Oct 30, 2021 4:40 am
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;

Re: http_req( but need the http code on failure

Posted: Mon Nov 01, 2021 1:53 pm
by taylorDH0210
Will try today. Thanks for the quick response.

Re: http_req( but need the http code on failure

Posted: Mon Nov 01, 2021 3:17 pm
by taylorDH0210
This worked great!

http_error(*omit: status);