1

I have created an endpoint that accepts a string in its request param

@GetMapping(value = "/validate")
    private void validateExpression(@RequestParam(value = "expression") String expression) {
        System.out.println(expression);
        // code to validate the input string
    }

While sending the request from postman as

https://localhost:8443/validate?expression=Y07607=Curr_month:Y07606/Curr_month:Y07608 

// lets say this is a valid input console displays as

Y07607=Curr_month:Y07606/Curr_month:Y07608 Valid

But when i send

https://localhost:8443/validate?expression=Y07607=Curr_month:Y07606+Curr_month:Y07608

//which is also an valid input console displays as

Y07607=Curr_month:Y07606 Curr_month:Y07608 Invalid

I am not understanding why "+" is not accepted as parameter.

"+" just vanishes till it reaches the api! Why?

Bhumika
  • 45
  • 2
  • 10

3 Answers3

0

I suggest to add this regular expression to your code to handle '+' char :

@GetMapping(value = "/validate")
    private void validateExpression(@RequestParam(value = "expression:.+") String expression) {
        System.out.println(expression);
        // code to validate the input string
    }
Rebai Ahmed
  • 1,509
  • 1
  • 14
  • 21
  • sorry it doesn't work, rather it throws exception stating [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'expression:.+' is not present] – Bhumika May 03 '21 at 09:30
  • Why you don't try to send your RequestParam like this : "Curr_month:Y07606+Curr_month:Y07608" , – Rebai Ahmed May 03 '21 at 09:36
  • I have to validate a whole expression i.e. LHS and RHS both. Simple the thing is it is replacing a '+' with a space because + is URL escape for spaces !! I still couldn't find any solution to handle it from back end – Bhumika May 03 '21 at 09:39
0

I didn't find any solution but the reason is because + is a special character in a URL escape for spaces. Thats why it is replacing + with a " " i.e. a space.

So apparently I have to encode it from my front-end

Bhumika
  • 45
  • 2
  • 10
0

Its wise to encode special characters in a URL. Characters like \ or :, etc. For + the format or value is %2. You can read more about URL encoding here. This is actually the preferred method because these special characters can sometimes cause unintended events to occur, like / or = which can mean something else in the URL.

And you need not worry about manually decoding it in the backend or server because it is automatically decoded, in most cases and frameworks. In your case, I assume you are using Spring Boot, so you don't need to worry about decoding.

Dame Lyngdoh
  • 312
  • 4
  • 18