HTTPPost multipart (upload file) from Java to Python webapp2
android, flask, google-app-engine, java, python
Solution
`blobstore.create_upload_url('/upload')` does not produce the url `/upload` yet in the java, /upload is where you are trying to post to look at the produced html from that handler and you will see the action url of the form is different.
You could have a handler respond with an upload url that your java code gets and then uses to post to.
Problem
I'm trying to do file upload to GAE - serverside code: ``` class UploadHandler(blobstore_handlers.BlobstoreUploadHandler): def post(self): upload_files = self.get_uploads('file') blob_info = upload_files[0] # When using flask, request.files[0] gives correct output. self.response.out.write('/serve/%s' % blob_info.key()) ``` With HTML, I can get it to work: ``` upload_url = blobstore.create_upload_url('/upload') self.response.out.write('<html><body>') self.response.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url) self.response.out.write("""Upload File: <input type="file" name="file"><br> <input type="submit" name="submit" value="Submit"> </form></body></html>""") ``` But I must be able to do a multipart post request from Java. I have a flask project hosted on openshift (flask, `request.files[0]` instead) in which the Java code appears to work: ``` HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); DefaultHttpClient mHttpClient = new DefaultHttpClient(params); HttpPost httppost = new HttpPost(getString(R.string.url_webservice) + "/upload"); MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multipartEntity.addPart("file", new FileBody(new File(path))); httppost.setEntity(multipartEntity); mHttpClient.execute(httppost, new MyUploadResponseHandler(path)); ``` But when I run it on GAE, im getting an index out of bounds on `upload_files[0]` when uploading - Where is the error? I cannot seem to find it, the code is so similar to the following which i have confirmed to work (flask, openshift): ``` def upload_file(): if request.method == 'POST': file = request.files['file'] ... ``` UPDATE: The complete error log: ``` list index out of range Traceback (most recent call last): File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__ rv = self.handle_exception(request, response, e) File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__ rv = self.router.dispatch(request, response) File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher return route.handler_adapter(request, response) File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__ return handler.dispatch() File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch return self.handle_exception(e, self.app.debug) File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch return method(*args, **kwargs) File "myapp.py", line 22, in post blob_info = upload_files[0] IndexError: list index out of range ```