Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RequestParam not mapping entire string

I have a Get endpoint that accepts a query string as request param . The issue with the endpoint it that the request param string can contain characters like ?,/ etc which is causing issues.Is there any way to map a string which contains ?,/ etc to a variable in a rest controller ?

Have already tried using @PathVariable instead of using @RequestParam but it is still not reading the value correctly. When using @PathVariable to map the uri , the ? is getting omitted. When using @RequestParam, it is throwing an Exception.Stacktrace is provided.

@GetMapping("/getResult")
public @ResponseBody ResponseEntity<String> getData(@RequestParam(value="text") String text) {
    //Call to service layer passing text as a param
    return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}

URL : 'localhost/getResult?text=How you doing?'

Stacktrace:

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:422) ~[tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:683) ~[tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455) [tomcat-embed-core-8.5.14.jar:8.5.14] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.14.jar:8.5.14] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_05] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_05] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.14.jar:8.5.14] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]

For the expected result , i would like to map the string "How you doing?" to a path variable or query param variable with the entire string intact, instead of the '?' getting omitted.

like image 590
Ananthapadmanabhan Avatar asked Jul 13 '26 06:07

Ananthapadmanabhan


2 Answers

If you are calling the url direct from browser or curl or postman. you should use encoding for different special characters like for space encoding is %20. That means when you write "how are you?" this would be "how%20are%20you%3F" in the url. Please refer to this site for more understanding.

And If the call is from any programming language. please use URL encoders.

like image 100
Muhammad Ahmed Avatar answered Jul 14 '26 20:07

Muhammad Ahmed


Encode the question mark(?) as %3F and you're good to go.

There's no other way you can send question mark in query as it is a special character.

like image 44
Milan Miljus Avatar answered Jul 14 '26 21:07

Milan Miljus