Endpoints and Payloads - Organizing API endpoints

本文阐述了如何通过将API端点组织基于资源而非动作,遵循一致、清晰的原则,如使用名词而非动词、保持简洁路径,来创建高效易用的API。重点讲解了如何设计GET、POST、PATCH和DELETE方法,并提供了实例说明良好和不良的API端点设计案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When organizing API endpoints, they should be based on the resources instead of on actions. The request methods will determine what action should be taken at a given URL endpoint. Your entire API’s scheme should be consistent, clear and concise.


在这里说一下,什么是API endpoints
In simple terms, an API endpoint is the point of entry in a communication channel when two systems are interacting. It refers to touchpoints of the communication between an API and a server. The endpoint can be viewed as the means from which the API can access the resources they need from a server to perform their tasks. An API endpoint is basically a fancy word for a URL of a server or service.

APIs operate through ‘request’ and ‘response’. And when an API requests to access data from a web application or server, a response is always sent back. The location where the APIs sends a request and where the response emanates is what is knowns as an endpoint. Reputedly, the endpoint is the most crucial part of the API documentation since it is what the developer will implement to make their requests.

An API refers to a set of protocols and tools that allow interaction between two different applications. In simple terms, it is a technical that enables third-party vendors to write programs that can easily interface with each other. On the other hand, an endpoint is the place of interaction between applications. API refers to the whole set of protocols that allows communication between two systems while an endpoint is a URL that enables the API to gain access to resources on a server.


API Endpoints 特点

  • Should be intuitive
  • Organize by resource
    • Use nouns in the path, not verbs
    • The method used will determine the operation taken
    • GOOD:
      • https://example.com/posts
    • BAD:
      • https://example.com/get_posts
  • Keep a consistent scheme
    • Plural nouns for collections
    • Use parameters to specify a specific item
    • GOOD:
      • https://example.com/entrees
      • https://example.com/entrees/5
    • BAD:
      • https://example.com/entree
      • https://example.com/entree_five
  • Don’t make them too complex or lengthy
    • No longer than collection/item/collection
    • GOOD:
      • https://example.com/entrees/5/reviews
    • BAD:
      • https://example.com/entrees/5/customers/4/reviews

ResourceGETPOSTPATCHDELETE
/tasksGet all tasksCreate a new taskPartial update of all tasksDelete al tasks
/tasks/1Get the details of task 1Error! (因为backend 有了)Partial update of task 1Delete task 1
/tasks/1/notesGet all the notes for task 1Create a new note for task 1Partial update of all notes of task 1Delete all notes of task 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值