0

I am using the node module 'request' to send up some JSON to my REST API.

I have the following call

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

I want to add to this - I would like to be able to act on a 429 status code - in order to make it retry the request until complete.

I know how to detect the 429 (using an if statement to check response.statusCode, etc), But I don't know how to make it retry, or if that is even the way to do it best.

4

1 回答 1

0

在我看来,您想要做的只是将所有这些代码包装在一个函数中,然后如果响应代码为 429,则让它自行调用。甚至可能包含一个“尝试尝试”数字作为该函数的最后一个可选参数,您然后可以记录你调用它的次数,并根据你的喜好采取行动

于 2015-11-15T22:09:16.433 回答