I'm encountering an issue where I am receiving a null response in Angular when attempting to post to a httpservlet (Java) in a WebSphere Application Server environment.
The objective is to call an API for logging in. However, I am getting a null response, which hinders any further action with the response.
I have tried sending my parameters as form data with the content-type application/x-www-form-urlencoded;charset=UTF-8 and as query parameters without specifying a content-type.
In both scenarios, I can successfully log in and receive a 200 response with header details in my browser.
This is the code snippet for my POST request:
return this.http.post(this.contextRoot.getHost() + 'osa-kantoor-ws/api/auth/login?' + param1 + "&" + param2,null, this.httpOptions).map((res : Response) => {
localStorage.setItem('currentUser', JSON.stringify(username));
return res;
});
The current header information set up is as follows:
httpOptions = {
headers: new HttpHeaders({'Content-Type' : 'application/json', 'Accept': 'application/text'}),
params: {}
};
While the local storage item is being set successfully, the response remains null.
Here is the subscribe function being used:
login() {
this.loginService.login(this.loginForm.value.userName,this.loginForm.value.password)
.subscribe(
data => {
console.log(data);
this.router.navigate([this.returnUrl]);
},
error => {
console.log(error);
this.loginfail = true;
}
);
}
Upon inspecting the browser information for the response header, I obtained the following details:
https://i.sstatic.net/JIorR.png
If anyone can provide insight into what might be causing this issue, it would be greatly appreciated.
Below is the original method that was referenced:
login(username: string, password: string) {
let param1: string = encodeURIComponent('userName') + '=' + encodeURIComponent(username);
let param2: string = encodeURIComponent('password') + '=' + encodeURIComponent(password);
let formParams: string = param1 + '&' + param2;
return this.http.post(this.contextRoot.getHost() + 'osa-kantoor-ws/api/auth/login', formParams, this.httpOptions).map(response=> {
localStorage.setItem('currentUser', JSON.stringify(username));
console.log(response);
return response;
});
}