72

At work we got a problem with case sensitive REST api which ignores wrongly spelled parameters without returning any error. In my opinion this is bad. Then it comes the general question:

Should a REST API be case sensitive or non case sensitive?

What are the advantages and disadvantages of each approach?

0

3 Answers 3

83

As others answered, the HTTP portion of the URL that makes APIs is case sensitive. This follows the UNIX convention, where URI paths were mapped to filesystem paths, and filesystem paths are case-sensitive. Windows, on the other hand, follows its convention of making paths case-insensitive.

However, it is bad practice in Unix to have two paths which only differ by capitalization; furthermore it is expected that paths be lower case.

Therefore: let's not break conventions.

and

should never coexist. Furthermore, products should be preferred to Products. Whether Products should return a 404, a 301 to products or simply be an alias of products is a matter of style -- it's your choice, but be consistent.

Stack Overflow APIs are canonically lower-case and case insensitive. I think this makes life easiest to the client, while having a clear default and being unsurprising for most users.

After all, can you think of a client that honestly benefits from case sensitivity and overlapping names?

Sign up to request clarification or add additional context in comments.

5 Comments

Awesome answer, as a side note tho, while SO has lowercase URLs, they are not case sensitive. Change the casing in the URL and it will land on the same page, and not redirect.
I'd also be interested in how SO are handling canicalisation - for instance, the canonical link tag on this link: stackoverflow.com/questions/21001455/… seems to point back to itself - wouldn't that cause content duplication issues for search engines?
@MatthewAbbott search engines disambiguate by looking at <link rel="canonical".../> or <meta property="og:url".../>. We also 301 many pages to their canonical URL.
@Sklivvz I understand that, but given that view-source:stackoverflow.com/questions/21001455/… and stackoverflow.com/questions/21001455/… are both returning the same content, and both have a canonical and open graph URL tag that point back to different URLs - that there is content duplication and there is no 301 in place? Surely regardless of case, these URLs should have the same canonical/og:url tag?
Any reference for "it is expected", "it is a bad practice"? We should back our answer in documented protocol, engineering specification, or the like facts to avoid opinion-based engineering.
43

HTTP URLs are case-insensitive for the scheme and host portion and case-sensitive for the path, query and fragment.

https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-p1-messaging-25#page-19

The scheme and host are case-insensitive and normally provided in lowercase; all other components are compared in a case-sensitive manner.

4 Comments

Ok, and how all of us the developers should design REST APIs? I see more problems designing case sensitive API.
@mynkow Actually REST makes it really easy to deal with case sensitive URIs. By using hypermedia with links and URI templates the server can ensure all URIs have the correct casing. The client never has to worry about it. The client just reads the URIs/URITemplates from the representation and follows them.
I up-voted this answer for the following reason: Web APIs are defined on top of HTTP, so they should respect HTTP conventions as much as possible. I agree with Darrel that Hypermedia is a useful approach to avoid issues due to incorrect spelling.
This does not actually answer the question in its current form.
9

At work we got a problem with case sensitive REST api which ignores wrongly spelled parameters without returning any error. In my opinion this is bad.

Then don't do that. Validate your parameters. Enforce "missing" parameters. Don't send bad requests in the first place. Conforming to an API, especially at such a level of spelling the parameters correctly, is not a gross burden.

Should a REST API be case sensitive or non case sensitive?

As has been mentioned, URLs are case sensitive, so there really isn't much room for negotiation here. Up/downshifting urls/parameters confuses everybody and it makes your URLs not unique. Again, it's not an extreme demand to expect implementors to use the proper URLs. These URLs are (most likely) not typed in by random people, they're implemented code or web pages. Finally, this only impacts the entry point URLs. The rest of the URLs should be direct copies lifted from the payloads because you're following HATEOAS. Those URLs shouldn't be messed with at all, and simply parroted back.

Simply, if case sensitivity is an issue, you're doing it wrong.

What are the advantages and disadvantages of each approach?

The advantages are consistency, clarity, and proper enforcement of your API. There are no disadvantages.

12 Comments

URLs are not case sensitive. File systems can be case sensitive, which has a ripple effect to the URL.
@phill Could you provide a reference because I am 99.9% sure that Rfc7320 says path and query for http URLs are case sensitive
@DarrelMiller tools.ietf.org/html/rfc7320 nether case-sensitive nor case-insensitive is used.
tools.ietf.org/html/rfc3986#section-6.2.2.1 "the scheme and host are case-insensitive and therefore should be normalized to lowercase ... The other generic syntax components are assumed to be case-sensitive unless specifically defined otherwise by the scheme" assumed because it depends on the implementation, i.e. its up to you.
@pbreitenbach, as mentioned earlier by Darrel, in RFC 7320 HTTP URLs are case sensitive, with exception of the host and scheme. Section 2.7.3 says "The scheme and host are case-insensitive and normally provided in lowercase; all other components are compared in a case-sensitive manner.". Query strings are part of the URL. POST parameters are solely dependent on how the resource interprets them, and are thus unspecified. Generic URIs do not specify, as the individual schemes govern everything after the scheme://hostname parts of the URI.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.