We developed an app on Ionic previously using Cordova which posted information to an API with headers set including credentials set as HTTPS_AUTH. This worked fine but we needed to update for a few reasons.
We have refactored this to a newer version of Ionic and Angular but it seems the headers are not being sent properly.
within the .ts we are bringing in the libraries:
import {HttpHeaders} from '@angular/common/http';
import {HttpClient} from '@angular/common/http';
Construct
constructor(private router: Router, private storage: Storage, private modalCtrl: ModalController, private http: HttpClient) {
We build the headers:
const headers = new HttpHeaders().set('HTTPS_AUTH', this.APIKey).set('example', '1')
.set('Content-Type', 'application/json; charset=utf-8').set('Access-Control-Allow-Origin', '*');
When I log the headers using
console.log(JSON.stringify(headers));
I get:
{"normalizedNames":{},"lazyUpdate[{"name":"HTTPS_AUTH","value":"xxxxxx","op":"s"},{"name":"example","value":"1","op":"s"},{"name":"Content-Type","value":"application/json; charset=utf-8","op":"s"},{"name":"Access-Control-Allow-Origin","value":"*","op":"s"}],"headers":{},"lazyInit":{"normalizedNames":{},"lazyUpdate":null,"headers":{}}}
But it seems that when I send the post to the endpoint:
this.http.post('https://api.xxxxxxxxx.com/api/v2/add_permit', body, headers ).subscribe(data => { **good results here** }, error => { **errors show here**};
I end up with the post being unauthorised before it gets a chance to view the body as the HTTPS_AUTH header is not being received.
I note that within the headers logging there is at the end an option of "headers":{} which suggests to me that the headers are not being set properly but I am at a loss how to.
I have attempted to create the headers directly:
const headers = { headers: { HTTPS_AUTH: this.APIKey, 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json; charset=utf-8'}};
but this gets blocked by CORS where the previous approach passed CORS
Any pointers gratefully received!
Many thanks