admin管理员组

文章数量:1291108

For uploading files from angular to a Spring MVC REST WebService, people seem to be setting Content-Type : undefined header in the AJAX request.

What is the purpose of this type of content-type and is it required by Spring MultiPart?

$http.post(uploadUrl, fd, {
  transformRequest : angular.identity,
  headers : {
   'Content-Type' : undefined
  }
//...
}

For uploading files from angular to a Spring MVC REST WebService, people seem to be setting Content-Type : undefined header in the AJAX request.

What is the purpose of this type of content-type and is it required by Spring MultiPart?

$http.post(uploadUrl, fd, {
  transformRequest : angular.identity,
  headers : {
   'Content-Type' : undefined
  }
//...
}
Share Improve this question edited Dec 18, 2018 at 7:53 Javier C. 8,2675 gold badges46 silver badges57 bronze badges asked Jan 12, 2017 at 7:46 Monish SenMonish Sen 1,8983 gold badges23 silver badges34 bronze badges 1
  • fast-forward to 2022 and Typescript 4x, with strict type checking one cannot set the Content-Type to undefined. Setting any other string value wont work. Therefore the content-type header must be omitted entirely. Angular HttpClient will implicitly set it to multipart – Monish Sen Commented Jul 16, 2022 at 8:27
Add a ment  | 

1 Answer 1

Reset to default 9

It will reset the default header 'application/json' and let the browser fill in the correct Content-Type for us, along with a correct boundary parameter.

Setting in manually to 'multipart/form-data', for example, will fail to add the already mentioned boundary.

本文标签: javascriptPurpose of ContentType undefined in AngularJS FileUploadStack Overflow