我正在使用节点模块'request‘向我的REST API发送一些JSON。
我有下面的电话
request({
uri: urlToUse, // Using the url constructed above.
method: "POST", // POSTing to the URI
body: JSON.stringify(jsonItems),
headers: // Allows us to authenticate.
{
'Content-Type' : 'application/json',
'Authorization' : auth
}},
//What to do after the request...
function(err, response, body)
{
// Was there an error?
if (err)
{
// If so, log it.
console.log(err);
}
// Did the server respond?
if (response)
{
// Log it.
console.log(response.statusCode);
// Did the response have a body?
if(body)
{
// Log it.
console.log(body);
}
}
});我想对此进行补充-我希望能够对429状态代码执行操作-以便让它重试请求,直到完成。
我知道如何检测429 (使用if语句检查response.statusCode等),但我不知道如何让它重试,或者这是否是做得最好的方法。
发布于 2015-11-16 06:09:16
在我看来,您想要做的就是将所有这些代码包装在一个函数中,然后如果响应代码是429,则让它调用自己。甚至可以将"try attempt“数字作为此函数的最后一个可选参数,这样您就可以对调用它的次数进行计数,并根据自己的喜好进行操作
https://stackoverflow.com/questions/33725167
复制相似问题