-1

I'm working on an API and am wondering if there are any advantages/disadvantages of accessing a record by passing the ID through the URL vs. as a parameter.

Example: Getting info of a message of ID 1

You could do this with example.com/messages/1 which would return the message of ID 1 in JSON format.

Or you could do example.com/messages?id=1.

Are there any reasons to choose one over the other or would it just be personal preferences?

unor
  • 92,415
  • 26
  • 211
  • 360
  • You should search for REST APIs and read about them. – dlondero Mar 03 '18 at 07:26
  • @dlondero Why? REST has nothing at all to say about what URLs should look like. It considers them as totally opaque. – Eric Stein Mar 04 '18 at 23:46
  • Are you kidding me? – dlondero Mar 05 '18 at 07:50
  • @dlondero Nope. REST says nothing about URLs. – Eric Stein Mar 05 '18 at 12:47
  • All the best practices around REST say how to properly define URLs in order to have collections and items in collections. – dlondero Mar 05 '18 at 15:54
  • @dlondero I respectfully suggest you may not understand the difference between REST and generic web APIs. "REST opaque URI" might be a good search, or you could look at the dissertation directly: https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm. You'll see it says nothing about URI design. In fact, there's a decent argument to be made that human-readable URIs are bad for HATEOAS-compliant APIs, because they encourage URI hacking instead of following links. – Eric Stein Mar 05 '18 at 16:01
  • I never wrote about finding them in Fielding's dissertation, you did. I speak about REST in practice. And I'll close it here :) Cya. – dlondero Mar 05 '18 at 17:03

1 Answers1

0

Your former example is a little more human-readable and intuitive. It will be easier for developers to work with on both sides of the API. It's also much more standard for web APIs. Most frameworks are designed to support URLs in both forms. All other things being equal, the first example is probably a better bet.

Eric Stein
  • 13,209
  • 3
  • 37
  • 52