[英]Multipart Post request in Apache Httpclient 4.5.6 not working
I am trying to upload a file to the server. 我正在尝试将文件上传到服务器。 The request is working fine while using Postman tool.
使用Postman工具时,请求正常。
In Postman I have : 在Postman我有:
POST :https://transport-stg.transperfect.com/api/files/upload?filename=test.png&targetFolderId=2ff5ea6a-8187-4622-ab58-c7511c445ae7
In Header I have : 在标题我有:
Content-Type = multipart/form-data
Authorixation = Bearer access_code
In Body I have: 在身体我有:
file = test.txt (this is file to be uploaded,able to select file location in Postman)
This Above is working as expected in postman tool. This Above在postman工具中按预期工作。
Now I wrote a below code in Java using HttpClient 4.5.6 as 现在我使用HttpClient 4.5.6编写了一个Java代码
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
File file = new File("C:\\Users\\lKadariya\\Desktop\\test.txt");
String authorizationValue = "Bearer "+accessToken;
HttpEntity data = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("srcUploader",file,ContentType.DEFAULT_BINARY,file.getName())
.build();
HttpUriRequest request = RequestBuilder.post("https://transport-stg.transperfect.com/api/files/upload")
.addHeader("Content-Type","multipart/form-data")
.addHeader("Authorization",authorizationValue)
.addHeader("Accept","application/json")
.addParameter("filename","test.txt")
.addParameter("targetFolderId","2ff5ea6a-8187-4622-ab58-c7511c445ae7")
.setEntity(data)
.build();
ResponseHandler<String> responseHandler = response -> {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
String responseBody = httpClient.execute(request, responseHandler);
And i am not able to upload the file. 我无法上传文件。 ERROr: Unable to upload file from server.
错误:无法从服务器上传文件。 the postman is giving the expected respone.
邮递员给予了预期的反响。
What could go wrong with this above Httpclient code? 上面的Httpclient代码有什么问题?
The OKhttp library is also working fine as OKhttp库也正常工作
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
// .addPart(
// Headers.of("Content-Disposition", "form-data; name=\"title\""),
// RequestBody.create(null, "test"))
.addPart(
Headers.of("Content-Disposition", "form-data; name=\"test\""),
RequestBody.create(MEDIA_TYPE_TXT, new File("test.txt")))
// .addFormDataPart("file","file.txt",
// RequestBody.create(MediaType.parse("application/octet-stream"),new File("test.txt"))
// )
.build();
Request request = new Request.Builder()
.header("Authorization", "Bearer " + accessToken)
.url("https://transport-stg.transperfect.com/api/files/upload?filename=basic.txt&targetFolderId=2ff5ea6a-8187-4622-ab58-c7511c445ae7")
.post(requestBody)
.header("Content-Type","multipart/form-data")
.build();
try {
Response response = client.newCall(request).execute();
What could go wrong in the HttpClient code? HttpClient代码中可能出现什么问题?
HttpUriRequest request = RequestBuilder.post("https://transport-stg.transperfect.com/api/files/upload")
.addHeader("Content-Type","multipart/form-data")
The Content-Type
value in your code is invalid as it does not include the boundary
parameter. 代码中的
Content-Type
值无效,因为它不包含boundary
参数。
You should not meddle with Content-Type
header at the request level and let HttpClient generate it automatically based on properties of the enclosed request entity. 您不应该在请求级别插入
Content-Type
标头,并让HttpClient根据所包含的请求实体的属性自动生成它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.