'How to return of the params to the backend (springBoot)?

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


Solution 1:[1]

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.

Solution 2:[2]

You need to use the @RequestParam annotation.

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jay Morelli
Solution 3 tricksSpecialist