0

I have endpoint used to find book by book reference.

The book reference is string that can contain white-space and any kind of special characters, e.g. mybook, my book, my-book, my/book, book++

// GET api/books/reference/{reference}
[HttpGet("reference/{reference}")]
public ActionResult<BookItem> FindByReference(string reference)

This is what I get when testing:

GET api/books/reference/mybook
OK

GET api/books/reference/my book
OK

GET api/books/reference/my-book
OK

GET api/books/reference/my+book
404 Not found

GET api/books/reference/my/book
404 Not found

GET api/books/reference/book++
404 Not found

What is a proper way to encode this reference parameter IN THE URL PATH so that it gets properly resolved by routing? Is that even possible?

Dusan
  • 5,000
  • 6
  • 41
  • 58
  • 1
    The caller needs to properly escape those characters. – JDB Dec 27 '18 at 17:26
  • Thanks, but - weird - if I use `UrlEncode` on the `my book` I get the `my+book` which results in 404. – Dusan Dec 27 '18 at 17:28
  • "api/books/reference/my book" seem to be wrong - I'd expect this to fail as space is not valid for path... As @JDB says the question is very strange - indeed Uri need to be constructed correctly first so testing invalid once is not really useful. Reading through RFC (or at least https://stackoverflow.com/questions/4669692/valid-characters-for-directory-part-of-a-url-for-short-links) could help with "valid" definition. – Alexei Levenkov Dec 27 '18 at 17:32
  • You pass url as query parameter (since you've used UrlEncode)? That is even more confusing. Could you post some [MCVE] of your client side code and server side code that handles that query parameter to call your FindByReference method? (Normally one would use `new Uri("http://fooo/bar var").AbsoluteUri` for just url) – Alexei Levenkov Dec 27 '18 at 17:36

1 Answers1

0

The encoding of the URLs is the responsibility of the client calling the API, a client must supply a valid URL if it wants a proper response. If you encode your examples you will get:

GET api/books/reference/mybook

GET api/books/reference/my%20book

GET api/books/reference/my-book

GET api/books/reference/my%2Bbook

GET api/books/reference/my%2Fbook

GET api/books/reference/book%2B%2B

These should now work.

If you want help with the actual encoding you will have to edit your question with the client source code.

matt_lethargic
  • 2,706
  • 1
  • 18
  • 33