In NodeJS and express I am handling a batch request to a OData API. Sometimes the OData service returns invalid results in the response which is denoted by a status 500 (could be more than one, since this is a OData batch response), even though the request header status code returns 200 because the connection to the API itself was successful.
I am new to NodeJS and am not sure how / the best way to detect these status 500 in the response body so that I can retry the request (reason is these 500 errors are usually timeouts and on the next retry goes away).
Do I need a package to be able to retrieve or intercept res.body? I don't see it available in express.
Request and response example
Request header
Request URL: https://odata/$batch
Request Method: POST
Status Code: 202 Accepted
Remote Address: xxxx
Referrer Policy: no-referrer-when-downgrade
Response
--batch_1564714041997_0
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 500 OK
Content-Type: text/plain;charset=utf-8
Internal server error
--batch_1564714041997_0
Content-Type: application/http
Content-Transfer-Encoding: binary
HTTP/1.1 500 OK
Content-Type: text/plain;charset=utf-8
Internal server error
--batch_1564714041997_0--
I would use a regular expression and a capturing group for the HTTP status code.
var regExForHTTPStatusCodeDetection = RegExp('HTTP\/1\.\\d (\\d{3})','g');
var requestNumber = 1;
while ((regExMatches = regExForHTTPStatusCodeDetection.exec(res.body)) !== null) {
// Get status code from first capturing group
var httpStatusCode = regExMatches[1];
// Output for above example:
// "Request 1 has returned HTTP 500."
// "Request 2 has returned HTTP 500."
console.log(`Request ${requestNumber} has returned HTTP ${}.`);
requestNumber++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.