简体   繁体   English

Angular 5 'Content-Type':'multipart/form-data' 被重置为 'application/json'

[英]Angular 5 'Content-Type': 'multipart/form-data' gets reset to 'application/json'

Hi I am having trouble trying to get my httpClient patch request to send FormData with content type 'multipart/form-data'.嗨,我在尝试获取 httpClient 补丁请求以发送内容类型为“multipart/form-data”的 FormData 时遇到问题。

I have specified the headers as shown:我已经指定了标题,如图所示:

 private httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'multipart/form-data', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' }) };

And I do the following to save to the nodejs server:我执行以下操作以保存到 nodejs 服务器:

 const formData: FormData = new FormData(); formData.append('materialFile', 'something'); return this.httpClient.patch<any>(this.apiUrl + loan.id, formData, this.httpOptions);

On my Nodejs/Express server I do the following:在我的 Nodejs/Express 服务器上,我执行以下操作:

 const Multer = require('multer'); router.patch('/:id', Multer().fields([{ name: "materialFile", maxCount: 1 }]), (req, res, next) => { console.log(req.files['materialFile']); });

When I inspect the request headers on the browser I see that the content-type is application/json.当我检查浏览器上的请求标头时,我看到内容类型是 application/json。

Anybody knows why?有谁知道为什么?

Angular tries to automatically set http header content-type according to request body, so there is absolutely no need to set it manually. Angular 会尝试根据请求体自动设置 http header content-type ,所以完全不需要手动设置。 If the content-type header is application/json in browser's devtools that means request body has been changed till angular's attempt to define the header.如果内容类型标头是浏览器开发工具中的application/json ,则意味着请求正文已更改,直到 angular 尝试定义标头。 That change most probably happens in interceptors.这种变化很可能发生在拦截器中。 So if you have an interceptor, that makes manipulation on the request, the issue is probably at that point.因此,如果您有一个拦截器,可以对请求进行操作,那么问题可能就在此时。

As Hikmat G. pointed out, the problem is most likely steming from an Http interceptor, in my case, i found out that in my Authorization interceptor that i use to inject the JWT Token was setting the content-type to application/json as shown below :正如 Hikmat G. 指出的那样,问题很可能源于 Http 拦截器,就我而言,我发现在我用来注入 JWT 令牌的授权拦截器中,将内容类型设置为application/json ,如图所示以下 :

    return req.clone({
        setHeaders: {
        'Content-Type' :  'application/json',
        'Accept': 'application/json',
        'Authorization': `Bearer ${ token }`,
        },
    });

and i did need it that way, except for a certain request where i was sending a file as part of a formdata object, where the content type had to be multipart/form-data , it would be automatically changed to json, so i added in a condition to avoid that problem and it seemed to be working :我确实需要那样做,除了某个请求,我将文件作为 formdata 对象的一部分发送,其中内容类型必须是multipart/form-data ,它会自动更改为 json,所以我添加了在避免该问题的情况下,它似乎有效:

        let json_blacklist = "/example/url";
        let reqHeaders = {
            setHeaders: {
            'Content-Type' :  'application/json',
            'Accept': 'application/json',
            'Authorization': `Bearer ${ token }`,
            },
        };

        if(req.url.includes(json_blacklist)) {
            delete reqHeaders.setHeaders["Content-Type"];
        }

        return req.clone(reqHeaders);

Tip : For requests where you're sending files, don't set the content-type to multipart/form-data, just remove the content type altogether and allow for the browser to set the content type by itself, this will help prevent webkit boundaries error as the browser will automatically set the boundaries, See #40561738 for more details.提示:对于发送文件的请求,不要将内容类型设置为 multipart/form-data,只需完全删除内容类型并允许浏览器自行设置内容类型,这将有助于防止 webkit边界错误,因为浏览器会自动设置边界,有关更多详细信息,请参阅#40561738

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Content-Type multipart/form-data 上传 IFormFile 时缺少内容类型边界 - missing content-type boundary when uploading IFormFile with Content-Type multipart/form-data Angular /.NET Core API - System.InvalidOperationException:'不正确的内容类型:应用程序/表单数据' - Angular /.NET Core API - System.InvalidOperationException: 'Incorrect Content-Type: application/form-data' 如何在angular 7中将内容类型设置为multipart/form-data - how to set content type to multipart/form-data in angular 7 在 Angular 中使用内容类型 multipart/form-data 发布 - Posting with content type multipart/form-data in Angular 如何从multipart / form-data正文请求中删除Content-Type? - How to remove Content-Type from multipart/form-data Body Request? 上传文件时,内容类型发送为“multipart/form-data”时找不到边界错误。如何解决此错误? - While uploading file getting error as boundary not found when content-type sent as “multipart/form-data”.How to solve this error? Angular 5-如何将null设置为HTTP POST multipart / form-data的内容类型 - Angular 5 - How to set null as content type for HTTP POST multipart/form-data 将 JSON 转换为 multipart/form-data angular/ionic - convert JSON to multipart/form-data angular/ionic Angular 2,Http和Not设置Content-Type“application / json” - Angular 2, Http and Not setting Content-Type “application/json” Angular 2 Post:不向服务器发送“内容类型:应用程序/json” - Angular 2 Post: Not sending 'content-type: application/json' to server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM