Reputation: 6226
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.
Upvotes: 3
Views: 2476
Reputation: 7185
You need to encode the parameters with special characters in the URL. You can use encodeURIComponent
from front end .
If you are using postman, you need to set following in Pre-request scripts,
var encoded = encodeURIComponent(postman.getEnvironmentVariable("text"));
postman.setEnvironmentVariable("encoded text", encoded);
Upvotes: 0
Reputation: 214
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.
Upvotes: 2
Reputation: 182
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.
Upvotes: 4
Reputation: 2369
Providing a Java solution:
You can use the URLEncoder
to achieve what you want:
URLEncoder enc = new URLEncoder();
enc.encode(String toEncode, String charset)
There is also a method that accepts only the string to be encoded, but this one is deprecated.
If you are calling from a Javascript client, you can use encodeURIComponent()
:
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
Upvotes: 3