Forum: Ruby "percent-encoding" and "application/x-www-form-urlencoded"

Posted by Jeff Tanner (jeff00seattle)
on 2012-09-22 07:41
Hi

From wikipedia on "percent-encoding":
http://en.wikipedia.org/wiki/Percent-encoding

"Percent-encoding, also known as URL encoding, ... is also used in the
preparation of data of the "application/x-www-form-urlencoded" media
type."

With the following code, do I need to make variable "requestJsonContent"
"percent-encoded" by calling "CGI.escape()"?;

    request = Net::HTTP::Post.new(uri.request_uri)
    request.body = requestJsonContent
    request["Content-Type"] = "application/x-www-form-urlencoded"

Here is an example of "requestJsonContent" value:

{"username":"third_party_int@bluefish.com","password":"redfish"}

Which when making it "percent-encoded" using "CGI.escape()":

%7B%22username%22%3A%22third_party_int%40bluefish.com%22%2C%22password%22%3A%22redfish%22%7D

If I send "requestJsonContent" as is (without making it
"percent-encoded")
to the remote service endpoint expecting "urlencoded", then the service
request succeeds. However, if I do make "requestContent" to be
"percent-encoded" by calling "CGI.escape()", then the service request
fails.

Curious, with the declaration of "request["Content-Type"] =
"application/x-www-form-urlencoded"", is this making "request.body" to
be "percent-encoded", and that is why I do not need to assign a
"percent-encoded" value?

Thanks

- Jeff in Seattle
Posted by 7stud -- (7stud)
on 2012-09-23 04:02
This is what you want to be reading:

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1

So yes the body of your POST must be encoded. The ruby docs give an 
example for creating an encoded POST request, so you might want to try 
that:

uri = URI('http://www.example.com/todo.cgi')
req = Net::HTTP::Post.new(uri.path)
req.set_form_data('from' => '2005-01-01', 'to' => '2005-03-31')

res = Net::HTTP.start(uri.hostname, uri.port) do |http|
  http.request(req)
end

case res
when Net::HTTPSuccess, Net::HTTPRedirection
  # OK
else
  res.value
end

http://ruby-doc.org/stdlib-1.9.3/
Posted by Matthew Kerwin (mattyk)
on 2012-09-23 04:15
(Received via mailing list)
I think there's some confusion here. The data you're sending looks like
JSON to me, which in this case is one format you could use to transmit 
two
key-value pairs.  Percent encoding is an alternative format.

I'd expect the percent-encoded equivalent to look like:
username=third_party_int%40bluefish.com&password=redfish

Whatever you used to construct that json string should be replaced with
some CGI.escape()-using algorithm.

*unless the server is actually expecting to receive a single entity 
(which
is a json string) -- in which case to be x-www-form-urlencoded you 
should
prefix the value (the json string) with a form field name, e.g.
"myjsonstring=" + CGI.escape(requestContent)

Cheers,
Matty

Sent from my android
Posted by 7stud -- (7stud)
on 2012-09-23 04:18
Anybody know where the method req.set_form_data appears in the ruby 
docs?
Posted by Robert Klemme (robert_k78)
on 2012-09-24 12:17
(Received via mailing list)
On Sun, Sep 23, 2012 at 4:18 AM, 7stud -- <lists@ruby-forum.com> wrote:
> Anybody know where the method req.set_form_data appears in the ruby
> docs?

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/ht...

robert
Posted by 7stud -- (7stud)
on 2012-09-25 01:30
Thanks.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.