Why is Spring de-coding + (the plus character) on application/json get requests? and what should I do about it?

前端 未结 3 1641
生来不讨喜
生来不讨喜 2020-12-18 01:16

I have a Spring application that receives a request like http://localhost/foo?email=foo+bar@example.com. This triggers a controller that roughly looks like this

3条回答
  •  旧时难觅i
    2020-12-18 01:55

    If you have this request:

    http://localhost/foo?email=foo+bar@example.com
    

    then the original is foo bar@example.com. If you say the original should be foo+bar@example.com then the request should be:

    http://localhost/foo?email=foo%2Bbar@example.com
    

    So Spring is working as supposed to. Maybe on client you should check if the URI is properly encoded. The client-side URL encoding is responsible for building a correct HTTP request.

    See encodeURI() if you generate the request in JavaScript or uriToString() if you generate the request in Spring.

    Build your request string (the part after ?), without any encoding, with unencoded values like foo+bar@email.com, and only in the end, before actually using it in GET, encode all of it with whatever is available on the client platform. If you want to use POST then you should encode it according to the MIME type of your choice.

提交回复
热议问题