Rails: Plus sign in GET-Request replaced by space

前端 未结 2 1746
南旧
南旧 2020-12-15 19:34

In Rails 3 (Ruby 1.9.2) I send an request

 Started GET \"/controller/action?path=/41_+\"

But the parameter list looks like this:

         


        
相关标签:
2条回答
  • 2020-12-15 19:41

    For POST-requests, (in case that's how some of you stumbled upon this question, like me) one might encounter this problem because one has encoded the data in the wrong way on the client side. Encoding the data as application/x-www-form-urlencoded will tell rails to decode the data as it decodes a URL, and hence replace + signs with whitespace, according to the standard RFC1738 as explained by @mu is too short

    The solution is to encode the data on the client side as multipart/form-data.

    In PHP, using cURL, this is done by taking into consideration the following gotcha:

    Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded. http://php.net/manual/en/function.curl-setopt.php

    You might wonder why I was using PHP on the client side (that's because the client in my example was another webserver, since I'm working on an API connection.)

    0 讨论(0)
  • 2020-12-15 20:00

    That's normal URL encoding, the plus sign is a shorthand for a space:

    Within the query string, the plus sign is reserved as shorthand notation for a space. Therefore, real plus signs must be encoded. This method was used to make query URIs easier to pass in systems which did not allow spaces.

    And from the HTML5 standard:

    The character is a U+0020 SPACE character
    Replace the character with a single U+002B PLUS SIGN character (+).

    0 讨论(0)
提交回复
热议问题