How to set Content-Type and Accept in angular2?
I am trying to send post call with content type(application/json) in header But for somereason it does not send, it always send text/plain; charset=UTF-8 in content type I am getting 415 Unsupported Media Type when I try to make a REST service call. I think i need to set the type and Content-type properly somehow it does not set from code what i fo Below us the header request
Accept
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding
gzip, deflate
Accept-Language
en-US,en;q=0.5
Content-Length
13
Content-Type
text/plain; charset=UTF-8
Host
enrova.debug-zone.com:8000
Origin
http://localhost:8000
Referer
http://localhost:8000/add
User-Agent
Mozilla/5.0 (Windows NT 10.0; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0
Code is below
import {Component, View} from 'angular2/angular2';
import { Inject} from 'angular2/di';
import {Http} from 'angular2/http';
export class AchievementsService {
constructor( @Inject(Http) private http: Http) {
}
getAchievementsOfType(type: string) : any {
var path = '/api/achievements/' + type;
return this.http.get(path);
}
getAllAchievements() : any {
var path = '/api/achievements';
return this.http.get(path);
}
addAnAchievement(newAchievement) {
//var path = '/api/achievements';
var path = 'http://test.com:8000/branch';
return this.http.post('http://test.com:8000/branch', JSON.stringify(newAchievement),{
headers: { 'Content-Type': 'application/json; charset=utf-8'} });
}
**Calling Class**
import {Component, View} from 'angular2/angular2';
import { _settings } from '../../settings'
import {FormBuilder, Validators, formDirectives, ControlGroup} from 'angular2/forms';
import {Inject} from 'angular2/di';
import {Router} from 'angular2/router';
import {AchievementsService} from '../../services/achievementsService';
@Component({
selector: 'add',
injectables: [FormBuilder]
})
@View({
templateUrl: _settings.buildPath + '/components/add/add.html',
directives: [formDirectives]
})
export class Add {
addAchievementForm: any;
constructor( @Inject(FormBuilder) private formBuilder: FormBuilder,
@Inject(Router) private router: Router,
@Inject(AchievementsService) private achievementsService: AchievementsService) {
this.addAchievementForm = formBuilder.group({
name: ['']
});
}
// This is the funtion that call post call written in achievementsService.ts
addAchievement() {
this.achievementsService.addAnAchievement(this.addAchievementForm.value)
.map(r => r.json())
.subscribe(result => {
this.router.parent.navigate('/');
});
}
}