I am developing an application with Ionic and Angular, I make a post request that returns me a token of the Authorization header, until then I was able to get this value normally with the following code:
data.headers.get('Authorization')
The problem is that it only works when I run the application in the browser; when i try to run on android emulator, it returns null.
It is important to say that the request ends successfully, but the data does not return.
Full code:
logIn(username, password) {
this.body = {
'username': username,
'password': password
};
this.apiProvider.getToken(this.body, this.headers).subscribe(
data=>{
this.storage.set('token', data.headers.get('Authorization'));
this.navCtrl.setRoot(SideMenuPage);
}, error=>{
console.log(error);
this.msgError = true;
}
);
}
My config.xml:
<access origin="*" />
<allow-navigation href="http://api.pet.com.br/*" />
<allow-navigation href="https://api.pet.com.br/*" />
<allow-navigation href="data:*" />
<allow-intent href="http://api.pet.com.br/*" />
<allow-intent href="https://api.pet.com.br/*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="http://*" />
<allow-intent href="https://*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
Proxy configuration:
"proxies": [
{
"path": "*/api",
"proxyUrl": "http://api.pet.com.br"
}
]
API Call:
export class ApiProvider {
private urlApi:string = "api";
constructor(
public http: HttpClient,
public plt: Platform) {
if(this.plt.is('mobileweb')) {
this.urlApi = "api";
} else {
this.urlApi = "http://api.pet.com.br";
/* If it's not run in the browser it calls the url without the
proxy configuration, when I call it from the proxy path in the
emulator it gets an HttpErrorResponse: http faiulure during
parsing for http://localhost/api/login.
I do not understand why it replaces with localhost where should
be the api url I think. */
}
}
getToken(body, headers) {
return this.http.post(this.urlApi + "/login", body, { headers: headers, observe: 'response'});
}
}