Search code examples
api-design

Update rosources using PATCH


Given a resource

id: string;
name: string;
createdDate: number;
consumedDate: number; // unix
rejectedDate: number; 

If I have some logic related to rejectedDate, can I use a rest Patch Method to update only rejectedDate?

Like PATCH /resource/:id/rejectedDate

body:  { rejectedDate: '2020-01-01' }

Is this bad practice?


Solution

  • You can use

    ROUTE: /resource/:id
    METHODE: PATCH
    BODY: { rejectedDate: '2020-01-01', .... other fields to update if needed }
    

    Actually, the best practice is to have

    • GET: /resources - for the getting a list
    • GET: /resources/:id - for the getting by id
    • POST: /resources - for the creating a new one
    • PATCH: /resources/:id - for the partially updating
    • PUT: /resources/:id - for the complete replacement of the item
    • DELETE: /resources/:id - for removing item by id