RESTful Web Services Part 3 - URI

Uniform Resource Identifiers (URI)

    REST APIs use URIs to address resources.

  • Good URI
            http://api.example.restapi.org/france/paris/louvre/leonardo-da-vinci/mona-lisa
  • Bad URI
            http://api.example.restapi.org/68ddo-a9d3-11eo-9fic-0800200cga66

URI Formats
    The rules are in format of a URI. RFC 3986*

    URI syntax example
    URI = scheme "://" authority "/" path [ "?" query] ["#" fragment ]

URI Format Rules
  • (/)  separator indicate a hierarchical relationship
            http://api.canvas.restapi.org/shapes/polygons/quadrilaterals/squares
  • Tailing (/) should not be included in URIs
            http://api.canvas/restapi.org/shapes/
            http://api.canvas/restapi.org/shapes
  • Hyphens (-) used to improve the readability URI
            //improve the readability of names in long path segments
            http://api.example.restapi.org/blogs/mark-masse/entries/this-is-my-first-post 
  • Underscores (_) should not be used in URIs
            //(_) Partially obscured or completely hidden by this underlining.
            http://api.example_restapi.org/blog/mark-masse/this-is-my-first-post
  • Lowercase letters should be preferred in URI paths
            //RFC 3986 defines URIs are case-sensitive to avoid unnecessary confusion, for instance
            http://api.example.restapi.org/my-folder/my-doc
            HTTP://API.EXAMPLE.RESTAPI.ORG/my-folder/my-doc
            http://api.example.restapi.org/My-Folder/my-doc
  • File extensions should not be included in URIs
            http://api.college.restapi.org/students/3248234/transcripts/2005/fall
            http://api.college.restapi.org/students/3248234/transcripts/2005/fall.son

Resource Modeling
  • ( /) separated path segment corresponding to unique resource
            http://api.soccer.restapi.org                                                -- Base Line
            http://api.soccer.restapi.org/leagues
            http://api.soccer.restapi.org/leagues/seattle
            http://api.soccer.restapi.org/leagues/seattle/teams
            http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet -- Bottom Line

Resource Archetypes
Resource archetypes helps consistently communicate the structures and behaviors.
  • Document (Base Archetype)
            / /identifiers a document resource
            http://api.soccer.restapi.org/leagues/seattle
            http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet
            http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet/players/mike
  • Collection
            / /server-managed directory of resource
            http://api.soccer.restapi.org/leagues/seattle
            http://api.soccer.restapi.org/leagues/seattle/teams
            http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet/players
  • Store
            //client-managed resource repository
            PUT /users/1234/favorates/Alonso
  • Controller
            //Executable functions, with parameters and return values; inputs and outputs; Controller names typically appear as the last segment in a URI path
            POST /alerts/245743/resend

URI Path Design
  • A singular noun for document names
            http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet/players/clau
  • A plural noun collection names
            http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet/players
  • A plural noun for store names
            http://api.music.restapi.org/artists/mikemassedotcom/playlists
  • A verb or verb phrase for controllers names
            http://api.college.restapi.org/students/morgan/register
  • Variable path segments substituted with identity-based Values
            http://api.soccer.restapi.org/leagues/{leaguesId}/teams/{teamId}/players/{playId}
            http://api.soccer.restapi.org/leagues/seattle/teams/trebuchet/players/21
  • CRUD function names should not be used in URIs
            Preferred:DELETE /users/1234
            What not to do:GET /deleteUser?id=1234
            GET /deleteUser/1234
            DELETE /deleteUser/1234
            POST /users/1234/delete
  • Define a query
            URI = scheme "://" authority "/" path [ "?" query][ "#" fragment ]
  • Query is an unique identification of a resource
            http://api.college.restapi.org/students/morgan/send-sms
            http://api.college.restapi.org/students/morgan/send-sms?text=hello
  • Query provide additional interaction capabilities
    • ad hoc searching
    • Filtering
  • Query component used to filter collections or stores
            GET /users
            GET /user?role=admin
  • Query component used to paginate collection or store results
            Parameters:pageSize (maximum number of contained elements)
            pageStartIndex (zero-based index of the first element)

            GET /user?pageSize=25&pageStartIndex=50

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值