Francesco Iannazzo
Francesco Iannazzo

Reputation: 626

Tomcat7 reading URLEncoded POST Parameters

Hello I'm trying to upload binary data using a HTML form to a REST Service using POST. I'm using the follwing javascript snipplet for sending the binary data.

xhr.open("POST", "http://localhost:8080/SomeRESTServices/rest/fileupload");
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send("file="+ encodeURIComponent(evt.target.result) );

encodeURIComponent encodes all content of the file.

On the Tomcat side i receive the binary data encoded. like that:

file=Hello%20man%20what%20is%20%0A%0A%0Agoing%20%0A%23%0A%0A%0A%0Aon.%0A%0A

So my Question now is. Must I decode the binary data on my own ? Or can do that the tomcat 7 server for me ? I read some posts about URIEncoded POST parameters and settings in the server.xml of the Tomcat server.

I set the URIEncoding="UTF-8" useBodyEncodingForURI="true" in the server.xml but it doesn't change anything.

I'm not sure this is the right way for decoding the POST parameters.

Upvotes: 0

Views: 873

Answers (1)

Mark Thomas
Mark Thomas

Reputation: 16615

URIEncoding only applies to the query string. useBodyEncodingForURI="true" will use the request body encoding to decode the query string. Neither is what you want.

Browsers should, but rarely do, specify the encoding of form data provided via POST. If you call request.setCharacterEncoding() before you call request.getParameter() (or any other method that triggers the reading if the parameter) then Tomcat will decode the parameters using your preferred encoding. If you don't set it, Tomcat will use ISO-8859-1.

Upvotes: 1

Related Questions