400 Bad request rest api (Upload a photo)

rajsingh

Joined: 2011-03-13
Posts: 4
Posted: Sun, 2011-03-13 09:31

Hi
I am getting the 400 when i am trying to upload the photo using java through gallery3 rest api.
here is my code snippet. Can you please help me

Quote:

httpPost.addHeader("X-Gallery-Request-Method", "post");

// Specify the request key: (_prefs == reference to shared preferences)
httpPost.addHeader("X-Gallery-Request-Key",
removed for public display);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");

// Multipart; pay particular attention to the BROWSER_COMPATIBLE
MultipartEntity mpEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

// The file we will be uploading
ClassPathResource dataResource = new ClassPathResource("/myimage.jpg");
File upFile = dataResource.getFile();
// File upFile = new File("myimage.jpg");

try {

// Create a JSON object to be used in the StringBody
JSONObject jsonObj = new JSONObject();

// Add some values
jsonObj.put("name", upFile.getName());
jsonObj.put("type", "photo");

// Add the JSON "part"
mpEntity.addPart("entity", new StringBody(jsonObj.toString()));
} catch (JSONException je) {
// Log.v(TAG, je.getMessage());
} catch (UnsupportedEncodingException ue) {
// Log.v(TAG, ue.getMessage();
}

// The file body, eventually added to the multipart entity
FileBody fBody = new FileBody(upFile);

// Add the part
mpEntity.addPart("file", fBody);

// Set the entity
httpPost.setEntity(mpEntity);

try {

// Execute the request "POST"
HttpResponse httpResp = httpClient.execute(httpPost);

// Check the status code, in this case "created"
if (httpResp.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {

}
// return true;
} catch (Exception e) // General; more specific ClientProtocolException
// & IOException
{
// Log.v(TAG, e.getMessage());
}

}

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Sun, 2011-03-20 16:51

What's in your gallery3/var/logs error logs on the server side? That might be helpful.
---
Problems? Check gallery3/var/logs
file a bug/feature ticket | upgrade to the latest code! | hacking G3? join us on IRC!