0

I have the following controller method to get array of ids and then remove but what I send from react does not caught here.

@DeleteMapping({"delete-user"})
public GenericResponse deleteUser(@RequestBody String[] ids, Errors errors) {
    if (errors.hasErrors())
        throw new ParseException();
    return userService.delete(Arrays.asList(ids));
}

React request snippet

yield call(userDeleteService, payload.ids)

What payload is

callback: undefined
ids: Array(1)
    0: "08ddc3f3-9df3-463f-8d95-25a4633f24b7"
length: 1

This is the request that is sent from react side.

http://localhost:8080/user/delete-user?0=08ddc3f3-9df3-463f-8d95-25a4633f24b7&1=11036b08-8daa-44ef-a557-9723f20b8911&
4
  • 1
    The request should look like this : http://localhost:8080/user/delete-user?ids=<id1>&ids=<id2> etc... the request u currently have is wrong... 0 is an index for the FE but for the java it's nonsense and definitely not an item from the ids array. Commented Jul 4, 2019 at 12:12
  • so how can I handle this situation? Commented Jul 4, 2019 at 12:14
  • I have no react expertise and can't help much here...On prima vista found this thread but not sure if it will be helpful in any way... Try to dig a bit I guess is what you could do... Commented Jul 4, 2019 at 12:16
  • Why are you passing ids as parameters, that is really bad.Try @BogdanSucaciu solution that is the proper way to implement this Commented Jul 4, 2019 at 13:06

1 Answer 1

2

Let's revise a bit your Endpoint.

You want to delete a user based on a list of ids:

  1. You are exposing the endpoint using the DELETE verb which is perfect!
  2. As part of good REST practices, you should use nouns instead of verbs in your endpoint definition. So exposing a DELETE endpoint at /users is better than exposing a DELETE endpoint /users/delete-user.
  3. Make sure that from the front'end you are actually using the DELETE verb and not GET, POST or anything else.
  4. It seems that you are sending the ids through 2 different methods: body and query param, choose one! I would choose the body since your desired result is to send multiple objects at a time.

To retrieve a list of ids you should wrap your array in a class:

public class DeleteUserDTO {
  private List<String> ids;

  // getter  + setter
}

And then use this object as the request body:

@DeleteMapping
public GenericResponse deleteUser(@RequestBody DeleteUserDTO dto, Errors errors) {
    if (errors.hasErrors())
        throw new ParseException();
    return userService.delete(dto.getIds());
}
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.