Home > Back-end >  Java APIRest - Postman error 400 bad request with GET method and JWT
Java APIRest - Postman error 400 bad request with GET method and JWT

Time:07-18

enter image description here

When I log in to my API via postman, a JWT is generated. With this jwt I create data for a specific user. But when I am connected with this user and his JWT, when I make a GET request from POSTMAN with the connected user's JWT, I get a 400 error saying that the request is bad. I do not understand why. My Tomcat server port IS NOT 8080 but 7777... Just bellow my controller with GET method :

        @CrossOrigin(origins = "*")
        @RestController
        @RequestMapping("/weights")
        public class WeightRecordController {
        
           
            @Autowired
            AppUserRepository appUserRepository;
        
            @Autowired
            PersonRepository personRepository;
        
            private final Logger logger = LoggerFactory.getLogger(WeightRecordController.class);
        
            public Long getAppUserConnectedId(Principal principal) {
                if (!(principal instanceof UsernamePasswordAuthenticationToken)) {
                    throw new RuntimeException(("User not found"));
                }
                logger.info("USER IS PRESENT IN DATABASE FROM FUNCTION 'getAppUserConnectedId()'");
                UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) principal;
                AppUser appUserFinded = appUserRepository.findByAppUsername(token.getName());
                return appUserFinded.getIdUser();
            }
        
            @GetMapping("/all")
            public ResponseEntity<List<WeightRecord>> getAllWeights(@RequestParam Principal principal) {
                logger.info("GET /weights/all");
                Long appUserConnectedId = this.getAppUserConnectedId(principal);
                Person personToShow = personRepository.findById(appUserConnectedId).orElseThrow();
                return new ResponseEntity<List<WeightRecord>>(personToShow.getWeightsList(), HttpStatus.OK);
            }
    }

CodePudding user response:

I don't know why Tomcat runs on port 8080 and you call API on port 7777, but it returns code 400. it means that you pass authentication of the app. your get request @RequestParam principal but it doesn't present on your request

CodePudding user response:

If you're using port 8080 on your tomcat, why are you using the port 7777 on Postman ?

Also, try adding the following property in your spring boot configuration logging.level.org.springframework.web=DEBUG you will get the exact reason for getting 400 Bad requests on console logs.

  • Related