簡體   English   中英

有 npm 模塊:如何重試 POST 請求?

[英]got npm module : How to retry for POST requests?

以下示例適用於 GET 請求,但不適用於 POST 請求。 我怎樣才能使它適用於 POST?

https://www.npmjs.com/package/got#retry

const got = require('got')
const retry = {
  retry: {
    retries: 3
  }
}
got('http://localhost:3000/retry', retry).then(({ body }) => {
  console.log(body);
}).catch((err) => {
  console.log(err);
});

重試次數為 3 的 POST 請求示例。如果要禁用重試,請將重試次數設置為 0。

const got = require('got');
start()
async function start() {
 var response = await request()
 console.log(response);  
}

async function request() {
try {
    const response = await got.post('https://example.com', { retry: { limit: 3, methods: ["GET", "POST"] } });
    return response.body
 } catch (error) {
    console.log(error.response.body);   
    return error
 }
}

對於如下所示的 POST 添加方法,默認 got 不支持 POST 重試

got.post('https://example.com', { retry: { limit: 1, methods: ["GET", "POST"] } });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM