具有多部分表单的 Angular 后请求具有错误的内容类型 [英] Angular post-request with a multipart form has wrong content type

查看:25
本文介绍了具有多部分表单的 Angular 后请求具有错误的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下函数将文件上传到带有 Angular 7 的 HttpClient 的服务器

 pushFileToStorage(productId, key, file: File): Observable

知道我做错了什么吗?谢谢

解决方案

我刚刚发现我正在处理的项目有一个 HttpInterceptor,它正在添加内容类型application/json";默认情况下,任何未设置内容类型的请求.这就是问题所在.

I'm using the following function to upload a file to a server with the HttpClient of angular 7

  pushFileToStorage(productId, key, file: File): Observable<any> {
    let formdata: FormData = new FormData();

    formdata.append('prod', file);
    let url_ = '/admin5/api/v1/product/images/upload?';
    url_ += "productId=" + encodeURIComponent("" + productId) + "&";
    url_ += "kind=" + encodeURIComponent("" + key);

    return this.http.post(url_,formdata);
  }

The problem I'm having is that the HttpClient sets the wrong content type header (application/json instead of "multipart/form-data") and so the server can't read the file. This is what I see on the developer tools

Any idea on what I'm doing wrong? Thanks

解决方案

I just found out that the project I'm working on has an HttpInterceptor that is adding a content-type "application/json" by default to any request that doesn't set the content type. So that was the problem.

这篇关于具有多部分表单的 Angular 后请求具有错误的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆