0

I'm using the python poster library to try to upload a form containing including an image to a servlet. Locally, it runs fine, but when I deploy to app engine, it doesn't recognize it as multipart content.

ServletFileUpload.isMultipartContent(request) returns false

Here's how I'm using the poster library:

register_openers()
datagen, headers = multipart_encode({"image": open(filename)})
request = urllib2.Request(url, datagen, headers)

The servlet checks to make sure it is Multipart, but it fails that check. What can I do to further debug?

Thanks, jean

*******update********* printing out the stack trace...here's what i get. It complains the content type header isnull

org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.(FileUploadBase.java:885) at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331) at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349) at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)

jeanh
  • 228
  • 2
  • 10

1 Answers1

0

If you're on Windows (or a pedant;-), open(filename) is the wrong way to open a binary file and might mess things up -- use open(filename, 'rb'). Apart from that, assuming of course that you continue with a urllib2.urlopen(request) which you've omitted, that your imports are correct, and that filename and url are properly set previously, then your code seems legit.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Thanks for the tip. Yes, I followed with urllib2.urlopen(request) and everything else is set up correctly and runs as expected when I run my appengine app locally. When deployed, it hits the servlet, but the servlet rejects the request since I had a check to make sure it's multipart. – jeanh Jul 17 '10 at 10:05
  • @jeanh, I wonder if that check is correct -- at the servlet, can you "dump" the exact byte stream it's getting (into a file, log, whatever) and peer at it to see how it differs from what's being sent? – Alex Martelli Jul 17 '10 at 15:08
  • also just checked, when running locally, request.getContentType looks fine. After I deploy to app engine, it is null. – jeanh Jul 19 '10 at 20:06