简体   繁体   English

当等效的curl成功时,Truevault请求在Android上使用实体生成器失败。 对更改代码有何建议?

[英]Truevault request failing using entity builder on android while the curl equivalent is successful. Any suggestions for changes in the code?

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 | 我正在尝试通过使用等效于curl https://api.truevault.com/v1/vaults/00000000-0000-0000-0000-0000-000000000000/blobs -X POST \\ -u [API_KEY | ACCESS_TOKEN]: \\ --form "file=@xray.pdf" \\ -H "Content-Type:multipart/form-data" ACCESS_TOKEN]:\\ --form“ file=@xray.pdf” \\ -H“ Content-Type:multipart / form-data”

as described on https://docs.truevault.com/BLOBs#create-a-blob 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. 使用等效的curl时,请求成功。 Any suggestions what I should change in java code ? 有什么建议我应该在Java代码中进行哪些更改?

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM