Hi everyone, been using Scott's code for over a year and what a great tool for the 400. I am having a weird problem and wonder if anyone else has encountered it. When I do the Post I check for a valid response code. If greater than 299 and if 3 attempts made, I send an error message to our pager duty email with recovery instructions. This code has worked flawlessly in pre-prod and production environments for over a year. When make code changes on our QA environment I am getting a 000 response code or the return code is not 1 for success. But, if you look at the http log it say 200 received. So because of the zero I am doing the attempt 3 time and sending the error email when in fact everything was consumer with a valid response code. Anyone seen this before? and thanks!! forgot to add I am checking >299 or 0 for error. I can remove the 0 but not sure if I would capture an invalid response.
Announcement
Collapse
No announcement yet.
Scott Klement HTTPAPI - gets 0 response code but log shows 200 and does the post
Collapse
X
-
I added the 0 response check during initial testing. Have never seen this in Beta or Prod environments. To clear QA I removed the 0 check but we are concerned we might miss a real error. I did try a 3 minute on the HTTP_TIMEOUT value with no change.
Thanks for any help.
// Attempt the Post a Maximum of 3 Times
FOR Count = 1 to 3;
// POST The JSON Data To EndPoint
rc = http_url_post(URL:%addr(Data):%len(Data):IFS:
HTTP_TIMEOUT:HTTP_USERAGENT:HTTP_CONTENT);
// Extract Return Code
Monitor;
Return_code = %Int((%subst(http_error:10:3)));
On-Error;
Return_code = 0;
EndMon;
// POST Returned HTTP Error Message for Code Greater Than 299
If rc <> 1
// And (Return_Code > 299) Or (Return_Code = 0);
And Return_Code > 299;
If Count = 3;
gvMsg = http_error;
Exsr ErrorHandler;
// If POST Failed, Sleep Before Retry
Else;
Comment
-
Okay, so the code you posted sets "return_code" to 0 when the monitor/on-error kicks in. That is YOUR code, it is not part of HTTPAPI.
This code is not properly written. It assumes that there will always be an HTTP response code in position 10 of the error message returned by HTTPAPI, and if not, will use a zero.
However:
1) HTTPAPI returns DOZENS of messages that never have HTTP response codes in them, nor were ever intended to.
2) Even with messages that do have a response code in it there is absolutelty no guarantee that it will be in position 10 of the error message.
Not sure who wrote this or why they did it this way... but that's not how HTTPAPI was intended to work.
However, the "rc" variable (the variable returned from http_url_post) would be a better choice for this. It will contain:
< 0 = error detected by HTTPAPI locally.
0 = time out
1 = success (same as HTTP code 200)
> 1 = HTTP response code
Try using that instead of getting the human-readable message and assuming that position 10 will have the HTTP response code.
Comment
-
Thanks Scott. I wrote the code and thought that was the way it worked. Pardon my ignorance but I learn from my mistakes.
Aside from pulling the response code, the first IF is if RC not equal to zero. This would indicate and error correct? Not sure what >1 response code is? Does that mean error? or that a Response code was sent in the message?
so, if I just say if RC is not equal to 1 I have and error and do my retries? I can easily code a 200 in the log file if I get a RC = 1.
Thanks again for responding.
Comment
-
Originally posted by EddiekAtlanta View PostAside from pulling the response code, the first IF is if RC not equal to zero. This would indicate and error correct? Not sure what >1 response code is? Does that mean error? or that a Response code was sent in the message?- RC < 0 = HTTPAPI discovered an error. This includes stuff like the network isn't available, DNS can't find the host, a file you asked to send isn't found, etc.
- RC 0 means that the network connection timed out.
- RC 1 means "success" (Equivalent to 200 OK, but no 200 is ever returned
- RC > 1 means that the HTTP server found an error or a "redirect". In this case the RC value will be the number from the HTTP server. For example, if the server sends "404 Not Found", then RC=404. This is why there is no need to parse the human-readable message, because you already have the number in RC.
Originally posted by EddiekAtlanta View Postso, if I just say if RC is not equal to 1 I have and error and do my retries? I can easily code a 200 in the log file if I get a RC = 1.
Code:rc = http_url_post(parameters here); if rc <> 1; error_msg = http_error(); // error occurred, show error_msg to user else; // success! endif;
Comment
Comment