开发者

How can I change my current API to restful API?

I have something like this.... Books: I have a php programme, that only accept post request only..., the link is something like this:

http://www.mydomain.com/books.php

If the user wanna to make request, they need do something like this: Create: params:

a=c             //a = action
d=a array of dataJson    //d = a defined books data in Json format
u=user id
s=session key

and I will return:

0 / !0     //0 = success case, !0 will give the error code

array of id //the new create items array id

Read: params:

a=r                //a = action
id=the books id      (optional) //read the books from book id
title= a books title (开发者_如何学Coptional) //read the books from title
author= an author    (optional) //read the books from author
u=user id
s=session key

//if no optional params, return all books.

and I will return:

0 / !0     //0 = success case, !0 will give the error code
arrayOfBooks //books in array in Json format

Update: params:

a=u                //a = action
id=the books id       //update the books from book id
d=a dataJson    //d = a defined books data in Json format
u=user id
s=session key

and I will return:

0 / !0     //0 = success case, !0 will give the error code

Delete: params:

a=d                //a = action
id=the books id       //delete the books from book id
u=user id
s=session key

and I will return:

0 / !0     //0 = success case, !0 will give the error code

Is this ok for my current design to move to the "REST" way? Thank you.


I think you should use standard Http Status codes instead of your error codes. You should use every single valuable parameter in the url in hierarchical order like this :

Create should be:

POST : http://www.mydomain.com/user/<userId>/books/create 
Http request body = your json data

Answer should be : 201 -> Created or 409 -> Conflict if there is one defined with the same title

If you use sessionId as an url parameter, you should use it in the url too. You can use Http response body to pass bookIds back in a json array.

Read should be:

GET http://www.mydomain.com/user/<userId>/books/<bookId|@all>/<title|@all>/<author|@all>

Update:

POST http://www.mydomain.com/user/<userId>/books/<bookId> 
Http request body = your json data. (updatable fields only)

Answer : use HttpStatusCodes as response statuses and data as bookId

Delete:

POST http://www.mydomain.com/user/<userId>/books/<bookId>/delete

Answer : use HttpStatusCodes
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜