2

I'd like to make a POST request from angular whereas the backend developer's waiting for a spring boot request like this, with 3 params :

@PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<?> importFile(
            @RequestParam(value = "type") Dto dto,
            @RequestParam(value = "booleanValue", required = false) Boolean booleanValue,
            @RequestParam("file") MultipartFile file)
            throws IOException {

In Angular side I'm trying to build a form data, but I'm unable to add the boolean when I write this :

importFile(fileToImport: FileToImport, booleanValue?: boolean) {
    const formData = new FormData();
    formData.append('type', fileToImport.type);
    formData.append('booleanValue', booleanValue);
    formData.append('file', fileToImport.file);
    return this.http.post('/import', formData);
  }

force has got an error : Argument of type 'boolean' is not assignable to parameter of type 'string | Blob'

So how can I put 3 arguments to respect the backend ?

Thanks for your help

2
  • What is the FileToImport type? Commented Mar 11, 2020 at 10:39
  • Sorry I made a mistake. It's my boolean I'm unable to add to my formData Commented Mar 11, 2020 at 10:41

1 Answer 1

1

You are trying to pass a boolean value into the formData.append function. You should instead convert it to a string.

importFile(fileToImport: FileToImport, booleanValue?: boolean) {
  const formData = new FormData();
  formData.append('type', fileToImport.type);
  formData.append('force', booleanValue.toString());
  formData.append('file', fileToImport.file);
  return this.http.post('/import', formData);
}

If your backend is particular about how boolean values are parsed, you may need a bit more treatment. Bear in mind that Javascript string representation of boolean values are "true" and "false".

Sign up to request clarification or add additional context in comments.

Comments

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.