Reputation: 3744
I have a spring boot admin server
and a angular-client
front part. I'm trying to send some data from my front to my server using HTTPClient but somehow I'm getting the following error during my request, but first here is my code :
POST request in angular-client
:
runBatch(id: number, stateUpd: string): Observable<HttpEvent<{}>> {
const req = new HttpRequest('POST', '/update_state', {id, stateUpd}, {
reportProgress: true,
responseType: 'text'
});
return this.http.request(req);
}
Controller in angular-client
:
changeBatchState(state: string): void {
if(this.selection.selected.length >= 1){
this.selection.selected.forEach(batchInstance =>{
if(batchInstance.batchState == 'CRASHED' || 'KILLED' || 'SUBMITTED'){
console.log(batchInstance.id + " setting to RUNNABLE...");
this.dataTableService.runBatch(batchInstance.id, state).subscribe(event => {
if(event.type === HttpEventType.UploadProgress) {
console.log('POST /update_state sending...');
}
else if(event instanceof HttpResponse) {
console.log('Request completed !');
}
});
}
else {
console.log(batchInstance.id + ' can not set to RUNNABLE');
}
});
}
}
Controller in admin server
:
@PostMapping("/update_state")
public ResponseEntity<String> batchChangeState(@RequestParam("id") int id, @RequestParam("stateUpd") String stateUpd) {
try {
log.i("INSIDE CONTROLLER");
log.i("BATCH INSTANCE ID : " + id);
log.i("UPDATE REQUESTED : " + stateUpd);
return ResponseEntity.status(HttpStatus.OK).body("Batch instance: " + id + " updated");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("Fail to update batch instance " + id);
}
}
and here the error I get during the request :
ERROR
Object { headers: {…}, status: 400, statusText: "OK", url: "http://localhost:8870/update_state", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:8870/update_state: 400 OK", error: "{\"timestamp\":1517473012190,\"status\":400,\"error\":\"Bad Request\",\"exception\":\"org.springframework.web.bind.MissingServletRequestParameterException\",\"message\":\"Required int parameter 'id' is not present\",\"path\":\"/update_state\"}" }
I don't understand where it comes from since I'm sending the id correctly in my POST request, any ideas ?
Upvotes: 1
Views: 6372
Reputation: 7221
const req = new HttpRequest('POST', '/update_state', {id, stateUpd}, {
will create a request where {id, stateUpd} are in the body, not in the queryParams.
You should do
// change : Body = null, and data are in options.params
runBatch(id: number, stateUpd: string): Observable<HttpEvent<{}>> {
const req = new HttpRequest('POST', '/update_state', null, {
reportProgress: true,
responseType: 'text',
params: new HttpParams().set('id', id.toString()).set('stateUpd', stateUpd);
});
return this.http.request(req);
}
Upvotes: 1