Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to parse the POST Request Body

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the context type set incorrectly?
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IE8 has a bug where an ajax call for POST only can be set to "text/plain" and not multipart.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want multi-part; that's for file uploading. The content-type should be application/x-www-form-urlencoded.
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never come across this bug. Do you have documentation you can point to?
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On IE8, when you send an AJAX post request, your content-type is application/x-www-form-urlencoded?
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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";
}
 
Greg Bag
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?

 
Bear Bibeault
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
The knights of nee want a shrubbery. And a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic