Bug 47410 - HttpServletRequest.getInputStream is null with Content-Type: application/x-www-form-urlencoded
Summary: HttpServletRequest.getInputStream is null with Content-Type: application/x-ww...
Alias: None
Product: Tomcat 6
Classification: Unclassified
Component: Servlet & JSP API (show other bugs)
Version: unspecified
Hardware: All All
: P2 critical (vote)
Target Milestone: default
Assignee: Tomcat Developers Mailing List
Keywords: APIBug
Depends on:
Reported: 2009-06-23 06:28 UTC by neng1998
Modified: 2009-06-23 06:45 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description neng1998 2009-06-23 06:28:08 UTC
When client send xml message with Content-Type: application/x-www-form-urlencoded. if you do "request.getParameter("message")" first. then subsequent call request.getInportStream()return an empty inputStream (not null, just not data).

1. If client set Content-Type: text/xml in request HTTP header, no problem.
2. If you do not do "request.getParameter("message")"; request.getInportStream() works. (can read the body data).
3. I understand the Content Type and client should not set "x-www-form-urlencoded" if they just send XML message in HTTP body. 

But, I still think it is bug.  request.getInportStream() should be able to read
the data in HTTP body regardless what Content Type is. 

Ed Xu

POST /ecpr/a2a/MessageDispatcher HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: ecprsit.wellsfargo.com:9001
Content-Length: 738
Expect: 100-continue
Connection: Close

<?xml version="1.0" encoding="utf-8"?>
  <Header version="1.0">

public void doPost(
		HttpServletRequest request,
		HttpServletResponse response)
		throws ServletException, IOException {	
		String message = request.getParameter("message"); // Cause input stream empty
	if (message == null) {
			// In case the client posted the data raw...
		int BUFSIZE = 4096;
		InputStream in = null;
		ByteArrayOutputStream byteStream = null;
		try {
			in = request.getInputStream();
			byteStream = new ByteArrayOutputStream(BUFSIZE);
			byte[] buf = new byte[BUFSIZE];
			int len;
			long READ_TIME_LIMIT = 2000;		
			while ((len = in.read(buf, 0, BUFSIZE)) != -1) {
            // Problem:
            // InputStream has nothing althoung the HTTP request contains body)
Comment 1 Mark Thomas 2009-06-23 06:45:15 UTC
As per section SRV.3.1.1 of the Servlet spec.