问题
This question is as an extension to my previous question here: "Is this web service Restfull" to an attempt to better understand the concept of a Rest Web Service. I read almost everything there is to be read about Rest and yet i am not able to understand if a certain Web Service is Rest or not , or why its not. The way i see it , everything is and is not Rest depending on the point of view...
I read in a very interesting article something that may at last make me understand what is Rest. I read that Rest doesnt use physical urls but logical ones.
I remember once i was developing an iOS application where we had to register/subscribe a user etc and i was told to make a certain Post HTTP request to our server to a URL that looked like this: www.myServer/devices/device/register. In the body of this request i had all the information in json format that the server needed. By that time the server for me was a black box so i didnt even care what this link meant or how it was generated. But i knew it was a web Rest service.
This month i started devepong a mobile hybrid app , where i started to develop in native code + jquery mobile for the front end , and php + mysql for the back end. I have more or less the same scenario here where the user needs to subscribe in an event or not. When he presses a button to subscribe to a specific event , i make an http post request with ajax call and a json file in the body , to a specific php script and that script writes/updates/deletes in the database accordingly.
The call looks something like this :
$.ajax({
type: "POST",
url: "http://192.168.4.113/Server_CityInfo/subscribe.php",
data: data,
contentType: "application/json; charset=utf-8",
//dataType: "json",
success: function(response) {
$.mobile.changePage( "dialog.html", { role: "dialog" } );
},
error: function(xhr, status, message) { alert("Status: " + status + "\nMessage: " + message); }
});
As you see i do use a physical url , to point to my php script to take that action. Even the html page that the user is in , is a physical page! Is a page like myUrl.com/eventX .
Does that mean that this web service i am building is not a Rest one?
How did they last year point me to logical URLs and i can only point to physical php scripts? I know that their server was in java , maybe thats why the urls dont look physical?
And in the end of the day lets say i have a 100 different events that the user can subscribe to. Of course i wouldnt make a 100 static html pages. BUT i would make 1 html page that would be dynamically generated. But still the link to that page would be physical like mySite.com/event.html. The only way i know to make a url to look logical is when u have an index.html file inside a folder and you point to that folder so that the url looks like myWebsite.com/myFolder.
So the question here is , how to build a logical url and is this what makes a service Restfull?
EDIT
Another thing that i read a lot is that Rest is using urls that describe resources and not operations!. Again though i fail to understand. For everything in my web service i use Post Request. I want for example to subscribe a user to an event , i make a post request at the url my.server.com/subscribe.php and in the body of this request i have a json file with the id of the user and the event name .
When i want to register a user i make a post request at the url my.server.com/register.php and a json in the body with the id of the user.
Is this scenario Rest and if not what is missing?
回答1:
I will try to explain REST as I understand.
RESTfull webservices are based on two factors:
- URLS - The urls should be based on the resources. Like: /app/students, /app/students/{id}
METHODS - The method defines the operation to be done on the url. Like
GET- To get resourcePUT- To insert a recordPOST- To update a recordDELETE- To delete a record.
There are ways to achieve restful urls using PHP see the answer here
来源:https://stackoverflow.com/questions/17209809/logical-and-physical-urls