I am trying to upload an image by creating a blob using the equivalent of curl https://api.truevault.com/v1/vaults/00000000-0000-0000-0000-000000000000/blobs \ -X POST \ -u [API_KEY | ACCESS_TOKEN]: \ --form "[email protected]" \ -H "Content-Type:multipart/form-data"

as described on https://docs.truevault.com/BLOBs#create-a-blob

HttpClient httpClient = new DefaultHttpClient();
Log.d(Kiwee.KIWEE_TAG,"Request is:"+request.getUrl());
//Request printed here is:
//https://api.truevault.com/v1/vaults/vault_id/blobs
HttpPost post = new HttpPost(request.getUrl());
String basicAuth = BASIC_AUTH+Base64.encodeToString(API_KEY.getBytes(),Base64.DEFAULT);
post.setHeader(AUTHORIZATION,basicAuth);
post.setHeader(CONTENT_TYPE,TYPE_MULTIPART_FORM_DATA);
file = new File(filePath);
//file path on android device of image taken
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.addPart("file",new FileBody(file));
post.setEntity(entityBuilder.build());
HttpResponse response = httpClient.execute(post);

Its ending up in a bad request. Following is the response:

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>

The request is successful when curl equivalent is used. Any suggestions what I should change in java code ?

1

There are 1 best solutions below

0
On

Resolved:

String basicAuth=BASIC_AUTH+Base64.encodeToString(API_KEY.getBytes(),Base64.DEFAULT);

change to

String basicAuth = BASIC_AUTH + Base64.encodeToString((API_KEY+":"+EMPTY_STRING).getBytes(), Base64.NO_WRAP);