Salman Salman - 28 days ago 14
Java Question

pass arithmetic operation with http get request

I am required to pass an arithmetic operation like

2+8
to a restful back-end and receive the result. I know that a simple operation can be handled in frontend using javascript, but i just want to follow the requirement.

I send the operations with the following uri:

http://localhost:8080/?question=2+5


and in the back-end i have:

@RequestMapping("/")
public String getAnswer(@RequestParam("question") String question){

System.out.println("recieved question is: "+question);
return botService.Evaluator(question);


}

When i print the
question
it is like
2 3
so there is no operation there.
And the component complains with:

javax.script.ScriptException: <eval>:1:2 Expected ; but found 5
2 5
^ in <eval> at line number 1 at column number 2


So, why the
+
is missing?
and how can i fix it?

Answer

Use the URLEncoder class to make sure any special characters are encoded safely for transport.