1

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 ?

3
  • Can you check your post request in developer console? You can try to send parameters like this: new HttpRequest('POST', `/update_state?id=${id}&stateUpd=${stateUpd}`,... Commented Feb 1, 2018 at 8:43
  • I'm getting this error trying like that : Error parsing HTTP request header right now debugging to give you the request form.. Commented Feb 1, 2018 at 9:22
  • 1
    it does not remplace ${id} and ${stateUpd} by their values Commented Feb 1, 2018 at 10:45

1 Answer 1

1
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);
      }
Sign up to request clarification or add additional context in comments.

10 Comments

params is of type HttpParams, I'm getting a type error doing like that.
what is your angular version ? See stackoverflow.com/questions/45210406/… for the good way of adding HttpParams
params: { id: id.toString(), stateUpd: stateUpd }
It should be like that but there is still a type error on id: id.toString() for some unknow reasons..
Types of property 'params' are incompatible. Type '{ id: string; stateUpd: string; }' is not assignable to type 'HttpParams'. Object literal may only specify known properties, and 'id' does not exist in type 'HttpParams'. (method) Number.toString(radix?: number): string
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.