2

I'm using JAX-RS and have the following POST method in my web service:

@Path("create")
public class Create {

    public Create() {

    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public PostBundle createNewUser(final UserCredentials userCredentials) {

        final String name = userCredentials.getName();
        final String mobile = userCredentials.getMobile();
        final String password = userCredentials.getPassword();

        if  (mobile != null && password != null) {
            PostManager postManager = new PostManager();
            Response response = postManager.createUserByMobile(name, mobile, password);

            if  (response.getStatus() == Status.ALREADY_EXISTS)
                return new PostBundle("409","Conflict",null);
            else if (response.getStatus() == Status.SUCCESS) {
                return postManager.getPostBundleByMobile(name, mobile, password);
            } else
                return new PostBundle("500",response.getMessage(),null);

        } else {
            return new PostBundle("400", "BAD REQUEST",null);
        }

    }
}

And I have the UserInfo.java as follows: -

public class UserCredentials {

    private String name;
    private String email;
    private String mobile;
    private String password;

    public UserCredentials() {

        name = null;
        email = null;
        mobile = null;
        password = null;

    }

//Usual getters and setters

............

}

I'm using Chrome's Postman and sending the following JSON with a POST request

{
    "name"      :   "Arijit Banerjee",
    "email"     :   "",
    "mobile"    :   "0123456789",
    "password"  :   "65656565"
}

I get an HTTP 405 Method Not Allowed. I don't understand why. I followed a tutorial online (this one), had a look at the suggestion given here, but nothing worked. I'm running my Web service on Apache Tomcat 8.

Community
  • 1
  • 1
Auro
  • 1,578
  • 3
  • 31
  • 62
  • Which web framework are you using? – nbrooks Oct 22 '16 at 09:31
  • Because, err, the POST method is not allowed on that URL. Nothing to do with the code you have posted. – user207421 Oct 22 '16 at 09:53
  • So how do I take care of this? – Auro Oct 22 '16 at 10:01
  • Does that mean I can never make a post request to the web app I just made on Heroku? – Auro Oct 22 '16 at 10:02
  • If you are following that tutiorial, I guess you will have a `MessageResource` class. Can you show us its code? – jalv1039 Oct 22 '16 at 10:10
  • It means you can never make a POST request to that URL without suitably modifying the webapp. – user207421 Oct 22 '16 at 10:12
  • Problem solved. All I had to do was change the URL I was making the request to from `xxx.heroku.com` to `xxx.herokuapp.com`. Didn't have the slightest idea that this could be the problem – Auro Oct 22 '16 at 10:14
  • Do you need to specify the @Path on the createNewUser method? – Jameson Oct 22 '16 at 10:19
  • So you were posting to the wrong URL and it came as a surprise when it didn't work? – user207421 Oct 22 '16 at 12:08
  • i wasn't aware of the fact that all `POST` & `PUT` requests had to be sent to `xxx.herokuAPP.com` until now because all the `GET` requests to `xxx.heroku.com` worked perfectly fine – Auro Oct 22 '16 at 18:55

0 Answers0