首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >node -在429错误时重试请求?

node -在429错误时重试请求?
EN

Stack Overflow用户
提问于 2015-11-16 05:18:01
回答 1查看 1.5K关注 0票数 0

我正在使用节点模块'request‘向我的REST API发送一些JSON。

我有下面的电话

代码语言:javascript
复制
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等),但我不知道如何让它重试,或者这是否是做得最好的方法。

EN

回答 1

Stack Overflow用户

发布于 2015-11-16 06:09:16

在我看来,您想要做的就是将所有这些代码包装在一个函数中,然后如果响应代码是429,则让它调用自己。甚至可以将"try attempt“数字作为此函数的最后一个可选参数,这样您就可以对调用它的次数进行计数,并根据自己的喜好进行操作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33725167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档