"percent-encoding" and "application/x-www-form-urlencoded"

Hi

From wikipedia on “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”:“[email protected]”,“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

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/

Anybody know where the method req.set_form_data appears in the ruby
docs?

On Sun, Sep 23, 2012 at 4:18 AM, 7stud – [email protected] wrote:

Anybody know where the method req.set_form_data appears in the ruby
docs?

robert

Thanks.

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