RESTful webservice API:如何處理100個音樂文件的Web URI路由

[英]RESTful webservice API : how to handle a web URI route for a 100 music files

I am a totally newbie to the world of REST API. from what I have read and I think I understand 2 main things .

我是REST API世界的新手。從我所讀到的,我認為我理解2個主要的事情。

1) REST uses the the same HTTP methods (GET PUT etc) for a specific resource on the server.

1)REST對服務器上的特定資源使用相同的HTTP方法(GET PUT等)。

2) Every resource on my server must be accessible from a unique URI.


So Combining 1 and 2, I can send a HTTP request to send a resource URI (eg. and a GET request which will then fetch Music song 1).


Now I have a simple HTML page which has links to 100 Music files on the server. The idea is that the client will click on a link and then Should be able to play/download the music file (I am not so concerned about the how part related to audio). So the server must provide the links to each Music file eg: .


My Question: Q1) In this way must the should the client be exposed to each and every URI for the resource. So When the hosting the html page should i put these links individually for each music file link


Q2) We need to have a route to each and every resource for the 100 music files on the server . If I am doing in Sinatra like do I need a do route for every of the 100 music files ? Something link '/do' /Music/1 , '/do' /Music/2 and so on

Q2)我們需要為服務器上的100個音樂文件提供到每個資源的路由。如果我在Sinatra這樣做,我是否需要為100個音樂文件中的每一個做一個路線?鏈接'/ do'/ Music / 1,'/ do'/ Music / 2等等

(Isn't this cumbersome and if I add another 100 music files , then I need to add another 100 routes in Sinatra).


Is this how the REST API would work, or have I totally got it wrong?

這是REST API如何工作,還是我完全弄錯了?

2 个解决方案


Imagine you have two servers.


  1. First server is your api server: (java, node.js, etc...) This REST-server will return a simple json response:

    第一台服務器是你的api服務器: / ...(java,node.js等...)這個REST服務器將返回一個簡單的json響應:

    "base_url"  : "",
    "tracks"    : [
                "name" : "track1",
                "file_name" : "track1.mp3"
                "file_size" : "3.43MB"
                "name" : "track2",
                "file_name" : "track2.mp3"
                "file_size" : "6.12MB"
  2. The second server: is your content delivery server (apache, nginx, etc...)


The idea is, that your api REST server should never send binary data like audio or image. It sends json/xml only.

我們的想法是,您的api REST服務器永遠不應該發送音頻或圖像等二進制數據。它只發送json / xml。

You can also send the full url for every audio file, but this information could be redundant.



ad 1)

When the hosting the html page should i put these links individually for each music file link


If you take REST seriously and if you see this HTML page as a representation of the collection resource of all music files, you should do this. One basic mechanism of REST is links for the client. The client should be able to just follow links without having to build them himself.

如果您認真對待REST,並且如果您將此HTML頁面視為所有音樂文件的集合資源的表示,那么您應該這樣做。 REST的一個基本機制是客戶端的鏈接。客戶端應該能夠只跟蹤鏈接而無需自己構建鏈接。

ad 2)

We need to have a route to each and every resource for the 100 music files on the server.


Every library that implements a RESTful server is able to have routes with path paramters. Usually such a route is defined using a pattern like



where {id} is the path parameter. The code that is bound to this route will then have available a variable named id. Using this approach you need only one route.




粤ICP备14056181号  © 2014-2021