【发布时间】:2021-07-27 19:29:50
【问题描述】:
我正在使用 .Net Core Web api 和 Angular 构建一个 POST 请求,其中我将一个带有上传文件的 json 对象发送到服务器。在 Postman 中一切正常,但是当我从 Angular 应用程序发出相同的请求时,我收到以下错误。
System.InvalidOperationException: Incorrect Content-Type: application/json
我的用户界面代码...
``
// 显示键/值对
console.log(Object.entries(frmData));//返回一个空数组!
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
}
)
``
网络 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));
}
更新 httpheader 后收到其他错误...
在这里我更新了标头以包含 responseType,但我仍然收到相同的 Failed to read the request form 错误。
//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
}
)
设置'Content-Type': undefined 而不是'Content-Type': 'multipart/form-data',可以防止请求完全发送到服务器!
更新响应类型...
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
}
)
【问题讨论】:
-
从角度你是附加文件然后向服务器发布请求吗?
-
您确实应该提供有关您尝试过的其他信息。没有任何示例,我猜这与您通过 Angular 请求发送的标头有关,默认情况下,Angular 发送
application/json作为内容类型。 -
我更新了我的问题以包含我的标题的屏幕截图。
-
@carltonstith 您的 api 会为您提供文本响应,因此您还需要在 post 请求中设置
response:text。或者,如果您有表单,那么您也可以使用 formData ,并将此表单数据传递给发布请求 -
@carltonstith
'Content-Type': 'multipart/form-data')你只需要设置这个,也可以为你工作
标签: .net angular asp.net-core-webapi