0

I have an API to be designed, this one require only one parameter then to be inserted into DB. So POST is the verb, but my doubt is on the parameter that will be passed as query param or path param ?

@RequestMapping(value = "/users-access/{userId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiOperation("Promotes user access")
public boolean promoteUserAccess(@PathVariable String userId)
        throws BadRequestException, UnAuthorizedException {
      //actions
}

What's the best practices ? thanks !

Aboodz
  • 1,271
  • 10
  • 20
Mirlo
  • 515
  • 5
  • 21
  • 1
    you should use "patch" method for partial update. – Akash Shah Jul 20 '20 at 11:59
  • 3
    Does this answer your question? [Post parameter in path or in body](https://stackoverflow.com/questions/42390564/post-parameter-in-path-or-in-body) – pirho Jul 20 '20 at 12:10

2 Answers2

1

You should send your data in the request body like below.

@RequestMapping(value = "/users", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.NO_CONTENT)
@ApiOperation("Promotes user access")
public boolean promoteUserAccess(@RequestBody Request request)
        throws BadRequestException, UnAuthorizedException {
      //actions
}

public class Request {

  private String userId;  

}
javaguy
  • 695
  • 2
  • 10
  • 28
1

If the parameter is meant to refer a resource then use path param. If the parameter is meant to filter the resource then use query param.

In your case, the userId refers to resource so you should use it as path param.

Generally,query param is used in GET requests to sort/filter the resources. Ex:

GET/users?id=5