无法发布请求多部分/form-data角
我正在尝试通过角度代码发布FormData。我正在使用HTTPINTEPCEPTOR添加请求标头。
这里是我的代码:
HTTPINTECTEPTOR ---
if (request.url.includes(baseUrl)) {
request = request.clone({
headers: new HttpHeaders({
'Request-Date': currentDate.toString(),
'Request-Id': requestId,
"Accept": "application/json" ,
param1: environment.encriptionEnabled
? requestData['param1']
: 'null',
locale: 'en',
}),
body: environment.encriptionEnabled
? requestData.ciphertext
: request.body
});
}
使用Postman我能够正确发布。但是通过代码会出现错误。
任何帮助将不胜感激。
谢谢
控制器方法---
@PostMapping(value = RequestURIConstant.SEND_EMAIL_WITH_ATTACHMENT, consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE })
public void sendEmailWithAttachment(@RequestPart("request") String request, @RequestPart("file") MultipartFile file) {
EmailNotificationRequest emailRequest = externalNotification.getJson(request);
LOG.info("Request received to send email");
externalNotification.sendMail(emailRequest, file);
}
在服务器上登录的错误---
content-
org.apache.tomcat.util.http.fileupload.fileuploadexception:如果我删除content-content- 按照某些帖子的建议输入,然后我会收到不支持的媒体类型错误
I am trying to post formData through angular code. I am using httpInterceptor for adding request headers.
Here goes my code:
httpInterceptor ---
if (request.url.includes(baseUrl)) {
request = request.clone({
headers: new HttpHeaders({
'Request-Date': currentDate.toString(),
'Request-Id': requestId,
"Accept": "application/json" ,
param1: environment.encriptionEnabled
? requestData['param1']
: 'null',
locale: 'en',
}),
body: environment.encriptionEnabled
? requestData.ciphertext
: request.body
});
}
Using postman I am able to post correctly. But through code it gives error.
Looks like I am doing something wrong. Any help will be highly appreciated.
Thanks
controller method ---
@PostMapping(value = RequestURIConstant.SEND_EMAIL_WITH_ATTACHMENT, consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE })
public void sendEmailWithAttachment(@RequestPart("request") String request, @RequestPart("file") MultipartFile file) {
EmailNotificationRequest emailRequest = externalNotification.getJson(request);
LOG.info("Request received to send email");
externalNotification.sendMail(emailRequest, file);
}
Error logged on server ---
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
If I remove Content-Type as suggested on some posts then I get unsupported media type error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
过夜后,我能够解决此问题。主要问题是我的内容类型以及我创建FormData对象的方式。
这是我有效的更改:
首先,我删除了我的httpintecceptor中提到的内容类型
After spending nights I was able to fix this. The main issue was my content-type and the way I was creating formData object.
Here is my changes which worked:
Firstly I removed the content-type mentioned in my httpInterceptor
Since i needed to mention content-type for each part of the multipart request. I constructed using blob as blob gives us priviledge to specify content-type