gpt4 book ai didi

node.js - Nodejs - 来自 OData $batch 的响应正文状态

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:37 24 4
gpt4 key购买 nike

在 NodeJS 和 Express 中,我正在处理对 OData API 的批量请求。有时,OData 服务会在响应中返回无效结果,由状态 500 表示(可能不止一个,因为这是 OData 批处理响应),即使请求 header 状态代码返回 200,因为与 API 本身的连接成功。

我是 NodeJS 新手,不确定如何/最好的方法来检测响应正文中的这些状态 500,以便我可以重试请求(原因是这些 500 错误通常是超时,下次重试时就会消失)。

我需要一个包才能检索或拦截 res.body 吗?我没有看到它以 Express 形式提供。

请求和响应示例

请求头

Request URL: https://odata/$batch
Request Method: POST
Status Code: 202 Accepted
Remote Address: xxxx
Referrer Policy: no-referrer-when-downgrade

回应

--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--

最佳答案

我将使用正则表达式和捕获组来获取 HTTP 状态代码。

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++;
}

关于node.js - Nodejs - 来自 OData $batch 的响应正文状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57319550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com