1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【REST】RESTfulなURI設計

Posted at

はじめに

RESTを勉強したのでそのなかのURI設計についてまとめる。

RESTfulなURI設計とは

RESTというwebサービスの設計モデルに則ったURI設計。

その1:短く入力しやすいこと

冗長なパスを含まない、短く入力しやすいパスにする
シンプルで覚えやすいものにすることで入力ミスを防ぐ


NG)https://api.example.com/service/api/search/
OK)https://api.example.com/search/

その2:人が読んで理解できること

人が読んで理解できるものにする。
できるだけ省略をしない表記にする。
国や文化が変わっても不変の表記にすることで誤認識を防ぐ


NG)https://api.example.com/sv/u/
OK)https://api.example.com/users/

その3:大文字、小文字が混在していないこと

大文字、小文字が混在していないこと。できるだけ全て小文字で表記する。

その4:単語はハイフンでつなげること

アンダースコアを使わず、単語はハイフンでつなげること。
アンダースコアはタイプライターで下線を引くために用いられるものであったかららしい。

その5:単語は複数形を利用すること

URIで表現しているのは「リソースの集合」であるから。

その6:エンコードを必要とした文字を使わないこと

URIから意味が理解できないため。

例:カタカナを使用した場合
NG)https://api.example.com/ユーザー
=> https://api.example.com/%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC

OK)https://api.example.com/users/

その7:サーバー側のアーキテクチャを反映しないこと

悪意あるユーザーに脆弱性を突かれる危険があるため。

例:phpが動いていることがわかる
NG)https://api.example.com/cgi-bin/get_user.php?=12345
OK)https://api.example.com/users/12345

その8:改造しやすいこと

システム依存の設計は意味が理解できないため。

例:システム依存のalpha、bataが使われている
NG)https://api.example.com/items/alpha/12345
NG)https://api.example.com/items/bata/12345

OK)https://api.example.com/items/12345

その9:ルールが統一されていること

一定のルールに従って設計することで間違いを防ぐため。

例:userId:12345の例
NG)
https://api.example.com/friends?id=12345
https://api.example.com/friends/12345/message/

OK)
https://api.example.com/friends/12345
https://api.example.com/friends/12345/message/

おわりに

RESTは2000年に提唱されたものであるため、意外と古く昨今のIT情勢と離れてしまった部分があるが、内容自体は大切なのでしっかり学んでおきたい。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?