I'm building a POST request using .Net Core web api and Angular where I'm sending a json object with an uploaded filed to the server. Everything works fine in Postman but when I make the same request from the Angular application, I'm getting the following error.
System.InvalidOperationException: Incorrect Content-Type: application/json
My UI code...
``
// Display the key/value pairs
console.log(Object.entries(frmData));//returns an empty array!
var options = {content: frmData};
//submit this after the job id is returned
let httpOptions = {
headers: new HttpHeaders(
{
//'Content-Type': 'application/json',
'Content-Type': 'multipart/form-data',
'Referer': 'http://localhost:4200',
'Origin': 'http://localhost:4200',
//'Accept': 'application/json',
'Accept': '*/*',
'Authorization':'Bearer '+ this.loadToken()
}
).set('content-type','application/json').set('content-length','6')
};
this.httpService.post("http://localhost:63052/api/Document_ADD", options,httpOptions).subscribe(
data => {
debugger
console.log(data);
},
(err: HttpErrorResponse) => {
console.log(err.message); //Show error, if any
}
)
``
Web API
[HttpPost]
public async Task<IActionResult> InsertCustomer_Document_ADDResult([FromForm] Customer_Document_ADD customer_Document_ADDED)
{
var reqFile = Request.Form.Files.First();
using (Stream stream = reqFile.OpenReadStream())
{
using (var binaryReader = new BinaryReader(stream))
{
var fileContent = binaryReader.ReadBytes((int)stream.Length);
customer_Document_ADDED.file_data = fileContent;
var result = await _repository.InsertCustomer_Document_ADDResult(customer_Document_ADDED);
return Ok(result);
}
}
//return Ok(await _repository.InsertCustomer_Document_ADDResult(customer_Document_ADDED));
}
Here is a screenshot of my headers.

Additional error received after updating httpheader...
Here I've updated the headers to include responseType, but I'm still getting the same Failed to read the request form error.
//submit this after the job id is returned
let httpOptions = {
headers: new HttpHeaders(
{
//'Content-Type': 'application/json',
'Content-Type': 'multipart/form-data',
'Referer': 'http://localhost:4200',
'Origin': 'http://localhost:4200',
'Accept': 'application/json',
//'Accept': '*/*',
'Authorization':'Bearer '+ this.loadToken()
}
),responseType: 'text' as 'text'
//.set('content-type','application/json').set('content-length','6')
};
this.httpService.post("http://localhost:63052/api/Customer_Document_ADD", options,httpOptions).subscribe(
data => {
debugger
console.log(data);
},
(err: HttpErrorResponse) => {
console.log(err.message); //Show error, if any
}
)
Setting 'Content-Type': undefined instead of 'Content-Type': 'multipart/form-data', prevents the request getting sent to the server completely!
Updated responseType...
console.log(Object.entries(frmData));//returns an empty array!
var options = {content: frmData};
//submit this after the job id is returned
let httpOptions = {
headers: new HttpHeaders(
{
//'Content-Type': 'application/json',
'Content-Type': 'multipart/form-data',
// 'Content-Type': undefined,//this disables this request from being sent
'Referer': 'http://localhost:4200',
'Origin': 'http://localhost:4200',
'Accept': 'application/json',
//'Accept': '*/*',
'Authorization':'Bearer '+ this.loadToken()
}
),responseType: 'text' as const
//.set('content-type','application/json').set('content-length','6')
};
this.httpService.post("http://localhost:63052/api/Customer_Document_ADD", options,httpOptions).subscribe(
data => {
debugger
console.log(data);
},
(err: HttpErrorResponse) => {
console.log(err.message); //Show error, if any
}
)

application/jsonas the content type.response:textin post request too. and and alternatively if you have form then you can use formData too , and pass this form data to post request'Content-Type': 'multipart/form-data')you need to set only this and will work for you too