How to make Get Request with Request param in Postman

2k views Asked by At

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?

3

There are 3 answers

0
Bhumika On BEST ANSWER

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

3
Rebai Ahmed On

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
    }
0
Dame Lyngdoh On

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.