[英]How to convert curl call with "-i --upload-file" into java Unirest or any other http request?
[英]Multipart File Upload Request Using Unirest in Java
我可以使用 REST 客户端 (Insomnia) 发布此请求。 但是,当我无法编写正确的代码在 Java 中执行相同操作时。 下面是我的失眠请求的样子。
下面是客户端生成的代码的样子。
HttpResponse<String> response = Unirest.post("http://172.16.6.15:5053/image-service/services/image-panel-service/panel/images?=")
.header("com.yatra.tenant.header.tenantid", "1051")
.header("content-type", "multipart/form-data; boundary=---011000010111000001101001")
.body("-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"imageFile\"\r\n\r\n")
.asString();
下面是我用 Java 编写的代码,但它不起作用。
try {
HttpResponse<String> response = Unirest.post("http://172.16.6.15:5053/image-service/services/image-panel-service/panel/images")
.header("com.yatra.tenant.header.tenantid", "1051")
.header("content-type", "multipart/form-data")
.field("imageFile", new File("Desert.jpg"))
.field("imageData", new File("ImageUploadRequest.json")).asString();
System.out.println(response.getBody());
} catch (UnirestException e) {
e.printStackTrace();
}
摘自他们的文档http://kong.github.io/unirest-java/#file-uploads
Unirest.post("http://httpbin.org")
.field("imageFile", new File("JellyFirst.jpg"))
.asEmpty();
try {
MultipartBody body = Unirest.post("http://localhost:4849/upload/images")
.field("name", "bingoo.txt")
.field("files", temp1)
.field("files", temp2)
.field("files", temp3);
HttpResponse<String> file = body.asString();
System.out.println(file.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
摘自https://www.programcreek.com/java-api-examples/?api=com.mashape.unirest.request.body.MultipartBody
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.