3

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.

Vikas
  • 6,868
  • 4
  • 27
  • 41
Ananthapadmanabhan
  • 5,706
  • 6
  • 22
  • 39

4 Answers4

4

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.

Muhammad Ahmed
  • 182
  • 2
  • 6
3

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

ItFreak
  • 2,299
  • 5
  • 21
  • 45
2

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.

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
Milan Miljus
  • 204
  • 1
  • 10
  • Yes, but make sure you only encode the parameters, not the entire URL! – ItFreak Apr 18 '19 at 09:35
  • 1
    %3F is a representation of question mark, I hope you understand that. Each special character needs to be encoded. Look here: https://secure.n-able.com/webhelp/NC_9-1-0_SO_en/Content/SA_docs/API_Level_Integration/API_Integration_URLEncoding.html – Milan Miljus Apr 18 '19 at 09:37
  • Also, you should use a library for encoding strings and not handle that yourself. – Milan Miljus Apr 18 '19 at 09:37
  • Got it , thank you.Slipped my mind that the query params had to be encoded since i was using postman to hit the endpoint and not through ajax. – Ananthapadmanabhan Apr 18 '19 at 09:40
0

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);
Vikas
  • 6,868
  • 4
  • 27
  • 41