Forums Register Login

How to parse the POST Request Body

+Pie Number of slices to send: Send
Hi guys, I need help on how to parse a post request body.

So I'm making a POST request from a CURLOPT script. The header's content-type is set to "text/plain", and it's not multipart(meaning I cannot use request.getParameter("name") to retrieve it..)

Let's say I pass this param1=aa¶m2=moofy¶m3=handz as parameters and I retrieve the post body with the following code..

String post_body = CharStreams.toString(request.getReader()); //This is a GUAVA library.

Problem is, I cannot do request.getParameter("param1") when the header is set as "text/plain".

What I really want to know is if there is some library out there that will parse the post_body string and return a map of the name/value pairs?
So like... HashMap<String, String> parameters = ParseUtil.parse(post_body).

System.out.println(parameters.get("param1")) would yield 'aa'

Thanks guys.
+Pie Number of slices to send: Send
Why is the context type set incorrectly?
+Pie Number of slices to send: Send
IE8 has a bug where an ajax call for POST only can be set to "text/plain" and not multipart.
+Pie Number of slices to send: Send
You don't want multi-part; that's for file uploading. The content-type should be application/x-www-form-urlencoded.
+Pie Number of slices to send: Send
sorry you're right, I meant to say that it cannot accept application/x-www-form-urlencoded. No matter what I have to use text/plain.
+Pie Number of slices to send: Send
I have never come across this bug. Do you have documentation you can point to?
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Hmm. I have issued POSTs (using jQuery) in many applications and I never ran across any issues. Have you actually tried it? The article mentions an IE8 beta.
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:Hmm. I have issued POSTs (using jQuery) in many applications and I never ran across any issues. Have you actually tried it? The article mentions an IE8 beta.



Have you tried with IE8 and the beta as well? Yes I've tried it.
+Pie Number of slices to send: Send
My apps have been run on stock IE8. Who would be running an IE8 beta?

I'd test it now but I don't currently have access to a virtual machine running Windows.
+Pie Number of slices to send: Send
On IE8, when you send an AJAX post request, your content-type is application/x-www-form-urlencoded?
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:I'd test it now but I don't currently have access to a virtual machine running Windows.

 
+Pie Number of slices to send: Send
OK, I fired up the virtual machine and ran a test. I think you're chasing a windmill -- the content type was 'application/x-www-form-urlencoded' as expected.

Here's the HTML I used to test:
The servlet at /testbed/inspect inspects all sorts of info from the request and environment (including the headers) and spits them out to its response. It shows that the content type is 'application/x-www-form-urlencoded' as expected even when loaded into IE8.

The results when executed from IE8:
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:OK, I fired up the virtual machine and ran a test. I think you're chasing a windmill -- the content type was 'application/x-www-form-urlencoded' as expected.

Here's the HTML I used to test:
The servlet at /testbed/inspect inspects all sorts of info from the request and environment (including the headers) and spits them out to its response. It shows that the content type is 'application/x-www-form-urlencoded' as expected even when loaded into IE8.

The results when executed from IE8:



Thank you for looking into this sir, greatly appreciate it. I will check again tomorrow and validate on my end.
+Pie Number of slices to send: Send
 

String post_body = CharStreams.toString(request.getReader()); //This is a GUAVA library.

Problem is, I cannot do request.getParameter("param1") when the header is set as "text/plain".



Well of course. You can't both get the body of the request AND use the request.getParameters() call.

The request.getParameters call assumes that the input stream is positioned at the start of the body but you already read that so no parameters are parsed.

Bill
+Pie Number of slices to send: Send
 

William Brogden wrote:

String post_body = CharStreams.toString(request.getReader()); //This is a GUAVA library.

Problem is, I cannot do request.getParameter("param1") when the header is set as "text/plain".



Well of course. You can't both get the body of the request AND use the request.getParameters() call.

The request.getParameters call assumes that the input stream is positioned at the start of the body but you already read that so no parameters are parsed.

Bill



No I know lol, I was trying to pinpoint how I'm getting the stream. The sentence structuring was off.
+Pie Number of slices to send: Send
So rephrasing the original question, the real problem that we are having is that we cannot submit custom headers in IE8.



Try to add an "Authorization" header and try to submit and retrieve it in the servlet.
+Pie Number of slices to send: Send
No problem.



Headers:
{
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded; charset\u003dUTF-8",
"connection": "Keep-Alive",
"host": "172.16.0.129:8080",
"accept-language": "en-us",
"content-length": "11",
"accept": "*/*",
"user-agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)",
"authorization": "whatever",
"accept-encoding": "gzip, deflate",
"x-requested-with": "XMLHttpRequest",
"referer": "http://172.16.0.129:8080/testbed/ie8-post.html"
}
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:No problem.



Headers:
{
"cache-control": "no-cache",
"content-type": "application/x-www-form-urlencoded; charset\u003dUTF-8",
"connection": "Keep-Alive",
"host": "172.16.0.129:8080",
"accept-language": "en-us",
"content-length": "11",
"accept": "*/*",
"user-agent": "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)",
"authorization": "whatever",
"accept-encoding": "gzip, deflate",
"x-requested-with": "XMLHttpRequest",
"referer": "http://172.16.0.129:8080/testbed/ie8-post.html"
}



Can you actually retrieve the parameter in the servlet and do like a System.out.println() ?
Are you using IE10 on IE8 mode?

+Pie Number of slices to send: Send
 

Greg Bag wrote:Can you actually retrieve the parameter in the servlet and do like a System.out.println()


Yes. No problem.

Are you using IE10 on IE8 mode?


No. This is IE8 (8.0.6001.18702, to be exact) running on Windows XP SP2.

As I've said, I have many apps that use Ajax up the wazoo, used by people using a large variety of browsers, including IE8, with no problems.

I suggest that you may want to download a known good instance of IE8. Yours appears to be hosed.
You can't have everything. Where would you put it?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 9714 times.
Similar Threads
Request parameter is missing
Difference between getAttribute and getParameter
Reading html form values using java script.
Unable to update database
jsp:setProperty equivalent in servlet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:03:54.