无法发布请求多部分/form-data角

发布于 2025-02-07 16:34:05 字数 1810 浏览 0 评论 0原文

我正在尝试通过角度代码发布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.

enter image description here

enter image description here

Looks like I am doing something wrong. Any help will be highly appreciated.

Thanks

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2025-02-14 16:34:05

过夜后,我能够解决此问题。主要问题是我的内容类型以及我创建FormData对象的方式。

这是我有效的更改:

  1. 首先,我删除了我的httpintecceptor中提到的内容类型

  2. 因为我需要在Multipart请求的每个部分中提及内容类型。我用blob构建了blob,使我们有特权指定内容类型


let formData = new FormData();
formData.append('request', new Blob([JSON.stringify(obj)],{type: "application/json"}));

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:

  1. Firstly I removed the content-type mentioned in my httpInterceptor

  2. 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


let formData = new FormData();
formData.append('request', new Blob([JSON.stringify(obj)],{type: "application/json"}));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文