Search code examples
androidpythonhttpurlconnectioncherrypycontent-length

Received length differs from sent length


I am using an Android app to send a base64 encoded string to a CherryPy server. The Android code works like this:

URL url = new URL("http://foo.bar/blabla");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/octet-stream");
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(base64s.length());

OutputStream out = new BufferedOutputStream(conn.getOutputStream());
out.write(base64s.getBytes());

So, you'd say the amount of bytes sent equals the amount of bytes in the Content-Length header. However, in Python, when I run this:

cl = cherrypy.request.headers['Content-Length']
rawbody = cherrypy.request.body.read()

print "{} bytes, {}".format(len(rawbody), cl)

The numbers cl and len(rawbody) are different.

How can that be?


Solution

  • Maybe you forgot to close the stream with out.close(); ?