Home > database >  Instant status: 400 "Bad request" upon attempting httppost
Instant status: 400 "Bad request" upon attempting httppost

Time:11-29

I have looked high and low (all over google/stackoverflow) for whatever could be wrong with my code... but nothing has turned out all day, so now I turn to writing a question myself: I have two near identical functions in my service class, that make a post request to my api/backend (which in turn contains two near identical functions to receive said requests). One works flawlessly, and the other doesn't even seem to "fire" off in the frontend before generating "status: 400." In my backend/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;
            }
        }

and in my service:

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))
        )
    }

and how they're being called:

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;
    }

And the two objects at hand:

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
}

(The objects being posted are the same object retreived from the corresponding classes' get-functions, with slightly altered lastName (for Patient) and commentText (for PatientComment)) I guess my question is: Am I missing something obvious? Is it the size of the Patient object that is too large, maybe? Again, it seems to me that the call doesn't even begin to process before I get status: 400 when updating Patient... and the post method in backend isn't even getting triggered - for PatientComment everything works, and I can trigger a breakpoint on its method in the backend whenever I call the endpoint. I've tested the api using both Swagger and Postman and they both seem to work there (though I am not super experienced using either of them, I guess, so I could be missing something). Any ideas?

I've triggered both api methods using Swagger/Postman, and I have debugged the process in VS Code - google'ing every part of the error message supplied from 'catchError' in the service class:

{"headers":{"normalizedNames":{},"lazyUpdate":null},"status":400,"statusText":"Bad Request","url":"https://localhost:62006/api/Patients/update","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://localhost:62006/api/Patients/update: 400 Bad Request","error":{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-f1e88aa13075736f6590b352c4afe68f-64f8c787e1bbcc8b-00","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."]}}}

I have then applied too many solutions to even count (most from other threads on stackoverflow), even when it mearly resembled a smiliar issue. The address to the api (localhost:whatever) is the same for both calls, and absolutely correct, and the endpoint has been copy/pasted from the backend just in case. I've tried supplying preflight data ( {headers: {'Content-Type':"application/json"}} ), using .put instead of .post, changing endpoint address setup, other localhost ports, JSON.stringify(editedPatient) as body... but nothing has worked (obv). The only thing I have been able to gather, since the breakpoint in the backend never fires, is that it is a frontend related issue... but at this point, I am barely sure of my own name :P

CodePudding user response:

in the error response it says that some fields are required on the Patient class, so when it's trying to bind the response body to the editedPatient parameter of your endpoint, your getting a 400 error b4 it can even get into the method.

so I'm guessing your most probably missing some of the Patient props from the body of the post request...

CodePudding user response:

The response contains the reasons why the request is rejected:

"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."
    ]
}

Set a breakpoint in pdatePatientAsync(Patient editedPatient) and find out why the object is not fully populated.

  • Related