Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to use for space in REST URI?

What should I use:

  • /findby/name/{first}_{last}
  • /findby/name/{first}-{last}
  • /findby/name/{first};{last}
  • /findby/name/first/{first}/last/{last}

etc.

The URI represents a Person resource with 1 name, but I need to logically separate the first from the last to identify each. I kind of like the last example because I can do:

  • /findby/name/first/{first}
  • /findby/name/last/{last}
  • /findby/name/first/{first}/last/{last}
like image 626
Droo Avatar asked Apr 09 '10 19:04

Droo


People also ask

How do you put a space in REST API URL?

Our recommendation is to avoid using spaces in URLs, and instead use hyphens to separate words. If you are unable to do this, make sure to encode whitespace using "+" or "%20" in the query-string, and using "%20" within the rest of the URL.

Can spaces be used in URI?

The URI generic syntax uses URL encoding to deal with this problem, while HTML forms make some additional substitutions rather than applying percent encoding for all such characters. For example, spaces in a string are either encoded with %20 or replaced with the plus sign ( + ).

How do you write a REST URL?

Creating a RESTful Web Service. A RESTful web service request contains: An Endpoint URL. An application implementing a RESTful API will define one or more URL endpoints with a domain, port, path, and/or query string — for example, https://mydomain/user/123?format=json .


2 Answers

You could always just accept spaces :-) (querystring escaped as %20)

But my preference is to just use dashes (-) ... looks nicer in the URL. unless you have a need to be able to essentially query in which case the last example is better as you noted

like image 130
Joel Martinez Avatar answered Oct 17 '22 18:10

Joel Martinez


Why not use + for space?

I am at a loss: dashes, minuses, underscores, %20... why not just use +? This is how spaces are normally encoded in query parameters. Yes, you can use %20 too but why, looks ugly.

I'd do

/personNamed/Joe+Blow
like image 40
Nas Banov Avatar answered Oct 17 '22 16:10

Nas Banov