1
votes

I have a Java Spring Boot application with a couple of API REST endpoints.

In one of my endpoints I am using as a search endpoint.

My problem is calling this specific endpoint with a path variable containing a slash "/" in it will give me a HTTP 400 Bad Request. The program doesn't seen to find the current mapping at all.

ie if the search string is foo/bar then the following call gives me a HTTP status 400 - Bad Request:

curl -X PUT "http://127.0.0.1:8080/search/foo%2Fbar" \
   -H "accept: application/json;charset=UTF-8" \
   -H "Content-Type: application/json" \
   -d "{ \"someSettings\": \"some value\", \"startNumber\": 0}"

This is how this part of the program looks like.

   @PutMapping(value = "/search/{searchString}",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
   public ResponseEntity<SearchResultModel> search(@PathVariable("searchString") String searchString, @RequestBody SearchSettingsModel searchSettings) {
      return handler.doSearch(searchString, searchSettings);
   }

The search works fine when I have have any other text string, and I prefer to use a PathVariable if possible. Is it possible to have the search in a PathVariable or do I have to put the string in the RequestBody instead?

I am using a Spring-Boot application with a Maven build with Java 11.

1
Unrelated, but shouldn't a search be a GET?Federico klez Culloca
You should probably do searches with GET rather than PUT.RealSkeptic
Have a look at this answer: stackoverflow.com/a/42403361/657224, or consider using a query parameter.Mike
You can find example of how to do this on: stackoverflow.com/questions/43470421/…. Additionally, you could use query params, or put value in the body as alternate options.alminh

1 Answers

2
votes

You can't have a %2F (= /) in your url.

Either add this to the body or as query parameter.