𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 - 𝗥𝗘𝗦𝗧 𝗔𝗣𝗜 𝗕𝗲𝘀𝘁 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲𝘀
𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 - 𝗥𝗘𝗦𝗧 𝗔𝗣𝗜 𝗕𝗲𝘀𝘁 𝗣𝗿𝗮𝗰𝘁𝗶𝗰𝗲𝘀
Article By: Radhakrishnan A Perumal
A REST API defines a set of rules that developers should follow when creating APIs.
While designing REST APIs seems straightforward, there are some common mistakes you should avoid in interviews👇
𝟭) 𝗨𝘀𝗶𝗻𝗴 𝘃𝗲𝗿𝗯𝘀 𝗶𝗻 𝗨𝗥𝗟𝘀
The endpoint URL should represent a resource (a noun), not an action (a verb). The HTTP method already indicates the action to be performed.
❌ GET /getAllBooks
❌ POST /createNewBook
✅ GET /books
✅ POST /books
𝟮) 𝗡𝗼𝘁 𝗨𝘀𝗶𝗻𝗴 𝗔𝗽𝗽𝗿𝗼𝗽𝗿𝗶𝗮𝘁𝗲 𝗛𝗧𝗧𝗣 𝗠𝗲𝘁𝗵𝗼𝗱𝘀
Using the appropriate HTTP method makes your API intuitive.
↳ GET = retrieve data
↳ POST = create new resources
↳ PUT = replace the entire resource with a new representation provided in the request body.
↳ PATCH = Used to apply partial modifications to a resource
↳ DELETE = remove resources
❌ POST /books/123/update to update a book with ID 123
✅ PUT /books/123 to update the book directly
𝟯) 𝗡𝗼𝘁 𝗨𝘀𝗶𝗻𝗴 𝗣𝗿𝗼𝗽𝗲𝗿 𝗦𝘁𝗮𝘁𝘂𝘀 𝗖𝗼𝗱𝗲𝘀
HTTP status codes are standardized codes that indicate the result of an HTTP request. Using them properly helps clients understand the outcome without parsing the response body.
❌ 200 OK for all responses
↳ 200 OK for successful GET, POST, PUT, and DELETE
↳201 Created for successful POST
↳204 No Content for successful DELETE
↳404 Not Found for resource not found
𝟰) 𝗜𝗻𝗰𝗼𝗻𝘀𝗶𝘀𝘁𝗲𝗻𝘁 𝗡𝗮𝗺𝗶𝗻𝗴 𝗖𝗼𝗻𝘃𝗲𝗻𝘁𝗶𝗼𝗻𝘀
Consistency in your API design helps developers predict endpoint structures, reducing confusion and errors.
❌ Mixing singular and plural nouns in endpoints → (/book/123, /authors)
✅ Consistently using plural nouns → (/books/123, /authors)
𝟱) 𝗡𝗼𝘁 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗶𝗻𝗴 𝗣𝗮𝗴𝗶𝗻𝗮𝘁𝗶𝗼𝗻
Pagination divides responses into manageable chunks, improving performance and user experience.
❌ Returning all records in a single response, which can be slow and resource-intensive
✅ Implementing pagination using query parameters like → ?page=2&limit=50
Comments
Post a Comment