即时状态:尝试 httppost 时 400 "Bad request"



我已经看了高低(全在google/stackoverflow)的任何可能是错误的我的代码…但是一整天都没有结果,所以现在我开始自己写一个问题:我的服务类中有两个几乎相同的函数,它们向我的api/后端发出post请求(后端又包含两个几乎相同的函数来接收所述请求)。一个工作完美无瑕,而另一个似乎甚至不"着火"。在生成"status: 400; "在我的后端/api:

[HttpPost("Patients/update")] //not working
public async Task<IActionResult> UpdatePatientAsync(Patient editedPatient)
{
try
{
_logger.LogDebug("APIController.UpdatePatientAsync() was called...");
var updated = await _dbHandler.UpdatePatientAsync(editedPatient);
if (updated)
{
return Ok(updated);
}
else
{
return BadRequest("Patient not updated!");
}
}
catch
{
throw;
}
}
[HttpPost("Patients/comment/update")] //works GREAT!
public async Task<IActionResult> UpdatePatientCommentAsync(PatientComment editedComment)
{
try
{
_logger.LogDebug("APIController.UpdatePatientComment() was called...");
var updated = await _dbHandler.UpdatePatientCommentAsync(editedComment);
if (updated)
{
return Ok(editedComment);
}
else
{
return BadRequest("Comment not updated.");
}
}
catch
{
throw;
}
}

和在我的服务:

updatePatient(editedPatient: Patient): Observable<Patient> { //not working at all
return this.http.post<Patient>(ConfigService.Config.apiBaseUrl + "/Patients/update", editedPatient).pipe(
catchError(this.rHndlr.handleError("updatePatient", this.updatedPatient))
)
}
updatePatientComment(editedComment: PatientComment): Observable<PatientComment>{ //works (again) GREAT!
return this.http.post<PatientComment>(ConfigService.Config.apiBaseUrl + "/Patients/comment/update", editedComment).pipe(
catchError(this.rHndlr.handleError("updatePatientComment", this.updatedComment))
)
}

以及它们是如何被调用的:

updatePatient(updatedPatient: Patient): Promise<Patient> {
this.loading = {
loadingText: "Updating patient",
errorText: "Comment update failed, try something else.",
errorTextVisible: false
}
const promise = new Promise<Patient>((resolve, reject) => {
this.patientSvc.updatePatient(updatedPatient).subscribe({ //NOT EVEN CLOSE TO WORKING!!!
next: (data: Patient) => {
if (JSON.stringify(updatedPatient) === JSON.stringify(data)) {
console.log("Success updating patient!")
}
},
error: (err) => {
alert("Error updating patient data!n" + JSON.stringify(err));
},
complete: () => {
resolve(this.patient);
}
})
});
return promise;
}
updatePatientComment(editedComment: PatientComment): Promise<PatientComment> {
this.loading = {
loadingText: "Updating comment",
errorText: "Comment update failed, try something else.",
errorTextVisible: false
}
const promise = new Promise<PatientComment>((resolve, reject) => {
this.patientSvc.updatePatientComment(editedComment).subscribe({ //WORKING!!!
next: (data: PatientComment) => {
if(JSON.stringify(editedComment) === JSON.stringify(data)){
console.log("Success updating comment!");
this.commentChanged = false;
}
},
error: (err) => {
alert("Error updating comment! n" + JSON.stringify(err));
},
complete: () => {
resolve(this.patientComment);
}
})
});
return promise;
}

和手边的两个对象:

export interface Patient {
id: number;
socialSecurityNumber: string;
firstName: string;
lastName: string;
diagnosisId: number;
riskAssessmentId: number;
deceasedDate?: number;
commentId: number;
clinicId: number;    
active: boolean;
staffId: number;
}
export interface PatientComment {
id: number,
commentText: string,
commentDate: Date,
signature: string
}

(发布的对象是从相应类的get-function中检索到的相同对象,只是lastName(用于Patient)和commentText(用于PatientComment)略有改变))我想我的问题是:我错过了什么明显的东西吗?也许是Patient对象的大小太大了?再一次,在我更新Patient时,在我得到status: 400之前,调用甚至没有开始处理。post方法在后端甚至没有被触发-对于PatientComment一切工作,我可以触发断点在它的方法在后端,每当我调用端点。我已经使用Swagger和Postman测试了api,它们似乎都可以在那里工作(尽管我不是非常有经验地使用它们中的任何一个,我猜,所以我可能会错过一些东西)。什么好主意吗?

我已经使用Swagger/Postman触发了这两个api方法,并且我已经在VS Code - google中调试了这个过程,在服务类中从'catchError'提供的错误消息的每个部分:

{"header ":{"normalizedNames":{},"lazyUpdate":null},"status":400,"statusText"; "错误请求","url";https://localhost:62006/api/Patients/update","ok":false,"name":"HttpErrorResponse","message":"Http https://localhost:62006/api/Patients/update:失败响应400坏Request"error"{"type"https://tools.ietf.org/html/rfc7231 # section-6.5.1","title":"一个或多个验证错误发生!","status": 400年,"traceId":"00 - f1e88aa13075736f6590b352c4afe68f - 64 f8c787e1bbcc8b - 00 -","errors":{"Staff"["员工字段是必需的!"),"Clinic":["诊所字段是必需的!"),"Diagnosis":["诊断领域required."],"PatientComment":["The PatientComment field is required."],"RiskAssessment":["The RiskAssessment field is required."]}}}

然后我应用了太多的解决方案,甚至计数(大多数来自stackoverflow上的其他线程),即使它几乎类似于一个类似的问题。api的地址(localhost:whatever)对于两个调用是相同的,并且绝对正确,并且端点已经从后端复制/粘贴以防万一。我试过提供预飞行数据({headers: {'Content-Type':"application/json"}}),使用。put而不是。post,更改端点地址设置,其他本地主机端口,JSON.stringify(editedPatient)作为主体…但是什么都没起作用(obv)。我唯一能收集到的是,由于后端断点从未触发,这是一个与前端相关的问题……但在这一点上,我几乎不确定自己的名字:P

响应包含请求被拒绝的原因:

"errors": {
"Staff": [
"The Staff field is required."
],
"Clinic": [
"The Clinic field is required."
],
"Diagnosis": [
"The Diagnosis field is required."
],
"PatientComment": [
"The PatientComment field is required."
],
"RiskAssessment": [
"The RiskAssessment field is required."
]
}

pdatePatientAsync(Patient editedPatient)中设置一个断点,并找出对象没有完全填充的原因。

在错误响应中,它说Patient类上需要一些字段,所以当它试图将响应体绑定到端点的editedPatient参数时,在它甚至可以进入方法之前,您将获得400个错误。

所以我猜你很可能从post请求的主体中错过了一些Patient属性。

最新更新