Reputation: 6005
I've searched how to set up Default Http Headers all my request and I found how to implement in in Angular 2 with bootstrap
function, but it's not available in Ionic 2, as described in this question How to set default HTTP header in Angular2?.
I want to set Content-Type
header to application/json
instead of setting it up in per-request
I have been trying to override BaseRequestOptions
and injecting them in my App, but I cannot get to override them.
app.ts
...
import {BaseRequestOptions, Headers, HTTP_PROVIDERS, Http, RequestOptions} from '@angular/http';
@Injectable()
export class MyRequestOptions extends BaseRequestOptions{
headers:Headers = new Headers({
'Content-Type': 'application/json; charset=UTF-8'
});
}
@App({
template: '<ion-nav [root]="rootPage"></ion-nav>',
config: {},
providers: [HTTP_PROVIDERS, MyRequestOptions]
})
export class MyApp {
rootPage: any = HomePage;
constructor(platform: Platform) {
// init
}
}
myService.ts
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
@Injectable()
export class MyService {
constructor(private http: Http) {
}
getUser(id) {
return this.http.post('/api/User/Get/', JSON.stringify({ id: id }));
}
}
There's something I'm missing, am I using wrong the providers
injection?
Upvotes: 2
Views: 2377
Reputation: 214305
You don't register MyRequestOptions
properly:
import { provide } from '@angular/core';
import { HTTP_PROVIDERS, RequestOptions, BaseRequestOptions, Headers } from '@angular/http';
...
@App({
template: '<ion-nav [root]="rootPage"></ion-nav>',
config: {},
providers: [HTTP_PROVIDERS, provide(RequestOptions, { useClass: MyRequestOptions })]
})
Or you can try to register provider via map literal like this:
{ provide: RequestOptions, useClass: MyRequestOptions }
Upvotes: 4