Do I need to encode the strings (like url) that I pass as a POST form parameter

Do I need to encode the strings (like url) I pass as a form parameter by POST?

Those. i want to pass the url i have for my web app (ruby on rails) as one of the form parameters. So are there potential characters in the URL / URI that need to be encoded? Or maybe rails can handle it?

+2


source to share


3 answers


Do I need to encode the strings (like url) I pass as a form parameter by POST?

It depends on what you are using to create / send your POST request. If you directly create the request body yourself, then yes, you will have to URL-encode each parameter:

POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded

foo=bar&url=http://www.example.com/url?innerparameter1=1&innerparameter2=2

      

this is not good: innerparameter2

it is actually an external formatted string parameter. This will require an encoding that looks like this:



foo=bar&url=http%3A//www.example.com/url%3Finnerparameter1%3D1%26innerparameter2%3D2

      

If, however, you are using something higher-level to make the POST request and pass in some sort of parameter string mapping, I would expect this component to take care of the url encoding for you.

code

+3


source


As bobince mentions, you need to encode any data you pass as URL parameters. Often, whatever library you use will take care of this. This applies to all BTW HTTP requests.

For example, an API has an endpoint GET /sites/:name

. Using cURL should look like

curl http://example.com/sites/google%2Ecom

      



In Ruby / Rails, you can use URI.encode

and URI.decode

:

>> URI.encode('google.com', /\W/)
"google%2Ecom"
>> URI.decode('google%2Ecom')
"google.com"

      

+1


source


As a general operator, if you have selected programmatic or custom input data to an HTML page, you must code it for HTML. Be aware that URLs often have an and and must be encoded, even though browsers seem to be able to handle it.

I'm not a Ruby guy, so I don't know how you do it in Ruby, and I'm not familiar with Ruby on Rails to say if it will (although I would be a little surprised by that), but the recommendation I am I suggest it does not depend on the language.

0


source







All Articles