Home > Software engineering >  How to return of the params to the backend (springBoot)?
How to return of the params to the backend (springBoot)?

Time:03-19

I am sending 2 params from the front end in the service below :

uploadFile(fileName: string, fileAsBase64:string): Observable<any> {
    let params = new HttpParams();
    params = params.append('fileName', fileName);
    params=params.append('fileAsBase64 ',fileAsBase64 )

    return  this.http.post('/api/uploadFile', params, { responseType: 'text' });
  }

But I receive only one paramaters: fileName=test fileAsBase64=null

@PostMapping(value ="/api/uploadFile")
public String uploadFile(@RequestBody String name , String fileAsBase64) {
   
}

CodePudding user response:

You could wrap your two parameter in a dto

@PostMapping(value = "/api/v1/upload-file")
public String uploadFile(@RequestBody EncodedFile file)
{

}

record EncodedFile(String fileName, String fileAsBase64) {}

And you better version your api.

CodePudding user response:

You need to use the @RequestParam annotation.

https://www.baeldung.com/spring-request-param

CodePudding user response:

you function have to be as the following:

@PostMapping(value ="/api/uploadFile")
public String uploadFile(@RequestParam String fileName, @RequestParam String fileAsBase64) {   
}

OR you can put any name but you have to put the matched name within the annotation:

@PostMapping(value ="/api/uploadFile")
public String uploadFile(@RequestParam(name = "fileName") String name, @RequestParam(name = "fileAsBase64") String file) {   
}
  • Related