Spring 使用RestTemplate发送二进制文件,cURL';s—数据二进制方式
我试图在Spring中实现与以下cURL命令等效的命令,以调用API(Twilio)上传媒体:Spring 使用RestTemplate发送二进制文件,cURL';s—数据二进制方式,spring,spring-boot,curl,resttemplate,twilio-api,Spring,Spring Boot,Curl,Resttemplate,Twilio Api,我试图在Spring中实现与以下cURL命令等效的命令,以调用API(Twilio)上传媒体: curl --header 'Authorization: Basic <VALID_BASIC_AUTH_TOKEN>' --data-binary "@test.png" https://mcs.us1.twilio.com/v1/Services/<ACCOUNT_ID>/Media -v curl--header'Authorization:Basic'--data
curl --header 'Authorization: Basic <VALID_BASIC_AUTH_TOKEN>' --data-binary "@test.png" https://mcs.us1.twilio.com/v1/Services/<ACCOUNT_ID>/Media -v
curl--header'Authorization:Basic'--data binary“@test.png”https://mcs.us1.twilio.com/v1/Services//Media -五
此请求工作正常,生成的标头为:
> Accept: */*
> Authorization: Basic <VALID_BASIC_AUTH_TOKEN>
> Content-Length: 385884
> Content-Type: application/x-www-form-urlencoded
>接受:*/*
>授权:基本
>内容长度:385884
>内容类型:application/x-www-form-urlencoded
我的java代码是:
@Repository
public class TwilioMediaRepository {
private RestTemplate client;
@Value("${twilio.chat.sid}")
private String chatSid;
@Value("${twilio.media.api.endpoint}")
private String endpoint;
@Value("${twilio.all.accountsid}")
private String accountSid;
@Value("${twilio.all.authtoken}")
private String accountSecret;
@Autowired
public TwilioMediaRepository(RestTemplate client) {
this.client = client;
}
public Media postMedia(byte[] file) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.ALL));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
client.getInterceptors().add(new RequestResponseLoggingInterceptor());
client.getInterceptors().add(new BasicAuthorizationInterceptor(accountSid, accountSecret));
HttpEntity<byte[]> entity = new HttpEntity<>(file, headers);
ResponseEntity<Media> media = this.client.postForEntity(generateUrl(), entity, Media.class);
return media.getBody();
}
private String generateUrl() {
return String.format(endpoint, chatSid);
}
}
@存储库
公共类斜纹夜总会{
私有模板客户端;
@值(${twilio.chat.sid}”)
私有字符串;
@值(${twilio.media.api.endpoint}”)
私有字符串端点;
@值(${twilio.all.accountsid}”)
私有字符串accountSid;
@值(${twilio.all.authtoken}”)
私钥;
@自动连线
公共TwilioMediaRepository(RestTemplate客户端){
this.client=client;
}
公共媒体后期媒体(字节[]文件){
HttpHeaders=新的HttpHeaders();
setAccept(Arrays.asList(MediaType.ALL));
headers.setContentType(MediaType.APPLICATION\u FORM\u URLENCODED);
client.getInterceptors().add(新的RequestResponseLogginInterceptor());
client.getInterceptors().add(新的BasicAuthorizationInterceptor(accountSid,accountSecret));
HttpEntity=新的HttpEntity(文件、标题);
ResponseEntity media=this.client.postForEntity(generateUrl(),entity,media.class);
返回media.getBody();
}
私有字符串生成器(){
返回String.format(端点,chatSid);
}
}
请求日志显示头与cURL请求完全相同:
URI : https://mcs.us1.twilio.com/v1/Services/<ACCOUNT_ID>/Media
Method : POST
Headers : {Accept=[*/*], Content-Type=[application/x-www-form-urlencoded], Content-Length=[385884], Authorization=[Basic <VALID_BASIC_AUTH_TOKEN>]}
Request body: <bunch of unreadable bytes>
URI:https://mcs.us1.twilio.com/v1/Services//Media
方法:邮寄
标题:{Accept=[*/*],内容类型=[application/x-www-form-urlencoded],内容长度=[385884],授权=[Basic]}
请求机构:
但是响应日志显示我的请求对Twilio无效:
Status code : 400
Status text : Bad Request
Headers : {Content-Type=[text/html], Date=[Wed, 08 Aug 2018 15:32:50 GMT], Server=[nginx], X-Shenanigans=[none], Content-Length=[166], Connection=[keep-alive]}
Response body: <html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
状态代码:400
状态文本:错误的请求
标题:{Content Type=[text/html],Date=[Wed,08 Aug 2018 15:32:50 GMT],Server=[nginx],X-Shenanigans=[none],Content Length=[166],Connection=[keep alive]}
答复机构:
400错误请求
400错误请求
nginx
我们应该如何发送二进制文件以符合Spring中cURL的--data binary
属性?
我试图发送HttpEntity
,HttpEntity
,HttpEntity
,HttpEntity
,但没有成功。
注意:此API不支持多部分文件上载。问题已解决。与cURL日志显示的相反,我使用的API不需要
应用程序/x-www-form-urlencoded
内容类型
使用文件内容类型解决问题(在我的例子中是image/png
)