Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

api - Whats the most RESTFul way to add resources?

For example, if there is a rest api for a shop system to add articles to my shopping cart without knowing the id, what would be the most restful way to design the url to call and what about the serverside implementation? I know there is some flexibility when designing rest apis, but what is "state of the art"?

Lets say my database does have the table "articles", "shoppingcarts", "article_pos". articles (id, article_name, price, color), shoppingcarts (id, description), article_pos (id, shoppingcart_id, article_id)

Example to create an article:

POST "/api/shop/article/" Request: { "article_name": "table", "price": "100.00", "color": "brown" }

Example to get an existing article:

GET "/api/shop/article/4711" Response: { "id": "4711", "article_name": "table", "price": "100.00", "color": "brown" }

But how I should design the url if I want to add article "4711" to the shopping cart?

Like this? POST "/api/shop/shoppingcart/addArticle/4711?amount=1"

or Like this? POST "/api/shop/article_pos/" Request: { "shoppingcart_id": "1", "article_id": "4711" }

And how to get the article ID if a client doesn't know the ID (4711) but the articlename (table)?

question from:https://stackoverflow.com/questions/65830669/whats-the-most-restful-way-to-add-resources

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...