0

Team,

I have created a search API and made it as GET request. Now when I try to give only a single space in search, it gives me 404 resource not found error. For example:

http://localhost:8080/myproject/myapp/search/ ?pageNo=1&limit=20

As you can see there is a space in the URL after /search/. This URL returns me the error 404. I'm using Spring 4.x version.

Thanks

Zeeshan
  • 177
  • 1
  • 15
  • Why the space? URLs are not supposed to have them. If you need a space as a part of something else, use the hex value or a `+`: `https://www.google.com/?q=my+name` – cst1992 Mar 29 '16 at 12:13

2 Answers2

2

The URL is invalid with a space between the URL part and the request parameter string '?'. You need to encode the URL to replace characters like space with '%20'.

Try:

String encodedUrl = URLEncoder.encode(url, "UTF-8");
pczeus
  • 7,709
  • 4
  • 36
  • 51
0

You can also trim all spaces before your query goes to request URL. Not sure that user need to have ability to search by space (nothing).

And using of URLEncoder for strings coming to URL is very recommended.

svichkar
  • 1,806
  • 1
  • 17
  • 23