Avoid duplicate POSTs with REST

前端 未结 7 1275
一生所求
一生所求 2020-12-02 06:34

I have been using POST in a REST API to create objects. Every once in a while, the server will create the object, but the client will be disconnected before it receives the

7条回答
  •  日久生厌
    2020-12-02 07:18

    You could try a two step approach. You request an object to be created, which returns a token. Then in a second request, ask for a status using the token. Until the status is requested using the token, you leave it in a "staged" state.

    If the client disconnects after the first request, they won't have the token and the object stays "staged" indefinitely or until you remove it with another process.

    If the first request succeeds, you have a valid token and you can grab the created object as many times as you want without it recreating anything.

    There's no reason why the token can't be the ID of the object in the data store. You can create the object during the first request. The second request really just updates the "staged" field.

提交回复
热议问题