0

I have a backend interface which I invoke with my Angular 1.3 application without problems. With my Angular 5 application I get an HTTP 403 (Forbidden) I faced the request parameters in an picture (Angular 1.3 at the left side, Angular 5 at the right side):

Angular5 vs Angular1.3

My Angular 5 code looks like this:

createDate(calendarEvent: CalendarEvent) {
    let serialDates = false;
    let calendarEventSerialDateType = 'NO_SERIAL_DATE';
    let serialEndDate = this.utilService.convertDateToDateString(new Date());
    let url: string = environment.apiEndpoint + 'calendarevents/calendarevent/' + serialDates + '/' + calendarEventSerialDateType + '/' + serialEndDate + '/';
    let headers = new Headers({ 'Content-Type': 'application/json', 'X-AUTH-TOKEN': this.authService.getToken()});
    let options = new RequestOptions({ headers: headers });
    return this.http.post(url, calendarEvent, options).map(res => res.json()).subscribe(res => console.log(res));

}

I have e.g. no idea why X-AUTH-TOKEN is not set with Angular 5 because I set it in the headers object with

let headers = new Headers({ 'Content-Type': 'application/json', 'X-AUTH-TOKEN': this.authService.getToken()});

and why OPTIONS is mentioned at Request Method with Angular 5 instead of POST like with angular 1.3.

Does anyone have any idea what I am doing wrong?

3 Answers 3

4
let Options = {
     headers: new HttpHeaders({
    'Content-Type': 'application/json'
  })
};

return this.http.post(url, calendarEvent, Options).map(res => res.json()).subscribe(res => console.log(res));

Sign up to request clarification or add additional context in comments.

1 Comment

@quma otherwise, create a service=> creating a file name as,<filename>.service.ts - Inside that perform your API call =>if you want to know more let me know.
0

OPTIONS request is considered as a pre-flight request, which is sent before the actual request to check the existence of method.

If the request sent is a valid one, it will call the valid method.

And regarding the request header, you can use the one in the above answer.

 let Options = {
     headers: new HttpHeaders({
    'Content-Type': 'application/json'
  })
    };

Comments

0

For Angular5,

import { HttpClient, HttpHeaders } from '@angular/common/http';

const headers = new HttpHeaders().set('X-AUTH-TOKEN', this.authService.getToken());
  1. By default, 'Content-Type': 'application/json'
  2. Stop using map.
  3. Subscribe the response and store it to Observable for further access.

Example:

createDate(calendarEvent: CalendarEvent) {
  let serialDates = false;
  let calendarEventSerialDateType = 'NO_SERIAL_DATE';
  let serialEndDate = this.utilService.convertDateToDateString(new Date());
  let url: string = environment.apiEndpoint + 'calendarevents/calendarevent/' + serialDates + '/' + calendarEventSerialDateType + '/' + serialEndDate + '/';
  let headers = new HttpHeaders().set('X-AUTH-TOKEN', this.authService.getToken());
  return this.http.post(url, calendarEvent, {headers: headers}).subscribe(res => console.log(res)); 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.