3

I start work with angular 2 and typeScript , all work great, but when i work with rest api (POST) in console log i get Response {_body: "", status: 204, statusText: "Ok", headers: Headers, type: 2… } , despite i passed the true param for login this is my code

 authentification(){
        var headers = new Headers();
          headers.append('Content-Type', 'application/x-www-form-urlencoded');
        // headers.append('Content-Type', 'application/json');
       return this.http.post(this._postUrl,JSON.stringify({username:"abc",password:"abc"}),{headers:headers});
    }

and this is my web service

 @POST
    @Path("/admin")
    @Consumes("application/x-www-form-urlencoded")
    public User getDate(@HeaderParam("username")String username,@HeaderParam("password")String password) throws Exception{
        try {
            String passwordC =  AESCrypt.encrypt(password);
            User u = userService.login(username, passwordC);
            return u;
            
        } catch (Exception e) {
            return null;
        }
    }

i think problem in my web service param in string and in angular param is json
any one can help me please and thanks.

1

1 Answer 1

7

For form-urlencoded content type you need to send data in query string format with key=value pairs. You can build this string with the help of URLSearchParams class from http module. In your case it can look something like this:

authentification() {

    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    var params = new URLSearchParams();
    params.append('username', 'abc');
    params.append('password', 'abc'); // .toString() => username=abc&password=abc

    return this.http.post(this._postUrl, params.toString(), {headers});
}
Sign up to request clarification or add additional context in comments.

1 Comment

Man love you. Just saved my a** from getting kicked :) GBY

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.