angularfile-uploadangular-httpclient

Angular post-request with a multipart form has wrong content type


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

enter image description here

enter image description here

Any idea on what I'm doing wrong? Thanks


Solution

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