0

I am trying to do a simple POST request but for some reason my Integer parameter is null. This is something so basic, but I don't see what I am doing wrong here.

Here is what I have tried so far:

$rootScope.addUser = function(userId) {
            $http.post('/addUser', {
                params: {
                    user_id: userId
                }
            }).then(function(result) {
                $rootScope.userId = undefined;
            });
        };

Controller

@PostMapping("/addUser")
public void addTournament(@RequestParam(required = false) final Integer userId) {
    LOG.info("addUser: {}" , userId);
}

I have also tried doing @RequestParam(name = "user_id") final Integer userId, but even that does not work either!

In the end I will removed the 'required = false' parameter, but I left it there for now just to verify that the userId is indeed null.

The input is being grabbed from the user they input a number and click a button.

1 Answer 1

1

The @RequestParam name does not match with the name of the attribute posted: user_id vs userId. Use @RequestParam(name = "user_id") final Integer userId to match them, or use the same request attribute and @RequestParam name.

[edit]

When I read params: { ... } I expected them to be additional request parameters instead of being part of the POST body. Could you try POSTing the data using:

$http.post('/addUser', { userId: userId }

And using the following @PostMapping:

@PostMapping("/addUser")
public void addTournament(@RequestBody User user) {
    LOG.info("addUser: {}" , user.getUserId());
}

...

class User {
private String userId;

// getter and setter
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply, I have tried that too but still no luck for some reason. The change I made was: @RequestParam(name = "user_id") final Integer userId, but now it does not print the log, so I am assuming it is still null for some reason...
I have updated my answer, I mistook the params attribute for additional request parameters instead of being part of the POST body.

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.