views:

501

answers:

2

Hi

Do I need to encode strings (eg URL) I pass as a POST form parameter?

Ie I want to pass a URL I have to my web application (ruby on rails) as one of the form parameters. So are there any potential characters in a URL/URI that would need to be encoded? Or perhaps rails would handle this anyway?

A: 

As a general statement, if you emit programmatic or user input data to an HTML page, you should encode it for HTML. Bear in mind that URLs often have the & character and that should be encoded, even if browsers appear to handle it okay.

I'm not a Ruby guy, so I don't know how you do that in Ruby, nor am I familiar with Ruby on Rails to say if it will do it (though I would be a little surprised by that), but the guideline I suggest isn't language specific.

John Cavan
In this case it will my client that does the update...so I was more trying to understand how robust a HTTP POST is with it's parameters re whether any characters are ok? Ie assume the input is a valid but complex URL/URI...hope this makes sense
Greg
@Greg Given the input could be anything, you should encode it. I'm assuming that you're emitting that value to an HTML document with that statment. If it's actually part of the HTTP protocol, no intermediate HTML document that has the URL embedded in it, then no, there's no specific encoding requirement I'm aware of.
John Cavan
+2  A: 

Do I need to encode strings (eg URL) I pass as a POST form parameter?

That depends on what you're using to create/send your POST request. If you're directly creating the request body yourself, then yes you would 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 no good:innerparameter2 is actually a parameter of the outer form-encoded string. It would need encoding, which would look like:

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 passing in some kind of mapping of parameter strings, I would expect that component to take care of the URL-encoding for you.

Code?

bobince