具有多部分形式的角后请求具有错误的内容类型 [英] Angular post-request with a multipart form has wrong content type
问题描述
我正在使用以下功能将文件上传到角度为7的HttpClient的服务器上
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);
}
我遇到的问题是HttpClient设置了错误的内容类型标头(application/json而不是"multipart/form-data"),因此服务器无法读取文件. 这就是我在开发人员工具上看到的
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
推荐答案
我刚刚发现我正在处理的项目具有HttpInterceptor,该HttpInterceptor添加了内容类型"application/json".默认情况下,对未设置内容类型的任何请求.这就是问题所在.
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.
这篇关于具有多部分形式的角后请求具有错误的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!