Home > Blockchain >  Java Spring - GET Request with two parameters
Java Spring - GET Request with two parameters

Time:12-27

I want to make a GET request to my server that receives two parameters, uniqueConfig and commitHash. The code for this operation in my Controller class is as follows:

@GetMapping("/statsUnique")
public ResponseEntity<Object> hasEntry(@RequestParam("uniqueConfig") String uniqueConfig,
                                       @RequestParam("commitHash") String commitHash) {
    Optional<Stats> statsOptional = 
        codecService.findByUniqueConfigAndCommitHash(uniqueConfig, commitHash);
    if (statsOptional.isPresent()) {
        return ResponseEntity.status(HttpStatus.OK).body(true);
    }
    return ResponseEntity.status(HttpStatus.OK).body(false);
}

The issue is, when I try to make the GET request using Postman, the server returns a 400 - Bad Request with the following error message:

MissingServletRequestParameterException: Required request parameter 'uniqueConfig' for method parameter type String is not present]

my JSON on Postman looks like this:

{
    "commitHash": "ec44ee022959410f9596175b9424d9fe1ece9bc8",
    "uniqueConfig": "bowing_22qp_30fr_29.97fps_fast-preset"
}

Please note that those aren't the only attributes and I've tried making the same request with all of them on the JSON. Nonetheless, I receive the same error.

What am I doing wrong here?

CodePudding user response:

A GET request doesn't (or at least shouldn't) have a body. Parameters defined by the @RequestParam annotations should be sent in the query string, not a JSON body, i.e., the request should be something like

http://myhost/statsUnique?commitHash=commitHash&uniqueConfig=bowing_22qp_30fr_29.97fps_fast-preset
  • Related