views:

855

answers:

1

I'd like to construct a multipart request, with the following parameters: name (string), email (string), and fileupload (file). I'm using the Java code below (working in Android).

The httppost.getRequestLine() prints

POST http://www.myurl.com/upload HTTP/1.1

So everything looks good on the client site, but my server (Django/Apache) reads it as a GET request, with no GET parameters - request.method produces 'GET', request.GET.items() produces an empty dictionary.

What am I doing wrong? I don't know actually how to set the multipart parameters correctly - am using guesswork - so it's possible that that's the problem.

public void SendMultipartFile() {
  Log.e(LOG_TAG, "SendMultipartFile");
  DefaultHttpClient httpclient = new DefaultHttpClient();
  HttpPost httppost = new HttpPost("http://www.myurl.com/upload");
  File file = new File(Environment.getExternalStorageDirectory(),
  "video.3gp");
  Log.e(LOG_TAG, "setting up multipart entity");
  MultipartEntity mpEntity = new MultipartEntity();
  ContentBody cbFile = new FileBody(file);
  mpEntity.addPart("fileupload", cbFile);
  Log.i("SendLargeFile", "file length = " + file.length());
  try {
   mpEntity.addPart("name", new StringBody(name));
   mpEntity.addPart("email", new StringBody(email));;
  } catch (UnsupportedEncodingException e1) {
   // TODO Auto-generated catch block
   Log.e(LOG_TAG, "UnsupportedEncodingException");
   e1.printStackTrace();
  }
  httppost.setEntity(mpEntity);
  Log.e(LOG_TAG, "executing request " + httppost.getRequestLine());
  HttpResponse response;
  try {
   Log.e(LOG_TAG, "about to execute");
   response = httpclient.execute(httppost);
   Log.e(LOG_TAG, "executed");
   HttpEntity resEntity = response.getEntity();
   Log.e(LOG_TAG, response.getStatusLine().toString());
   if (resEntity != null) {
    System.out.println(EntityUtils.toString(resEntity));
   }
   if (resEntity != null) {
    resEntity.consumeContent();
   }
  } catch (ClientProtocolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
A: 

Seems likely that you're looking in the wrong place. You're POSTing, but looking for data in request.GET:

Try looking for QueryDict's at "request.POST" and "request.FILES"...

http://docs.djangoproject.com/en/1.1/ref/request-response/#django.http.HttpRequest.FILES

John Mee