2

Lately I have got some small problems with headers and receiving REST API passed data. So Here is my UserService with method getUserData which sends the API a header with Authenticated User token and server checks if it's correct and passes the user data which should be received with : 'Content-Type', 'application/json'

Okay, here is the angular2 UserService

import { Injectable } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/toPromise';

import { User } from '../../models/user';

@Injectable()

export class UserService {

    public apiUrl: string = 'http://127.0.0.1:8000/api'; // without a slash
    public userData: any = localStorage.getItem('currentUser');
    public token:any = JSON.parse(this.userData);
    public result: any;
    private headers: any;
    private request: any;
    private handleError:any = 0;

    constructor( private http: Http, private route: ActivatedRoute ) {}


    getUserData():Promise<Array<User>>{

        this.headers = new Headers();
        this.headers.append('Authorization', 'Bearer {'+ this.token.token +'}');
        this.headers.append('Content-Type', 'application/json');


        this.request = new RequestOptions({headers:this.headers});

        return this.http.get( this.apiUrl + "/get/active/user-data/",  this.request)
        .toPromise()
        .then(( response ) => {
            console.log(response.json());
            debugger;
            return response.json().data as User[];

        })
        .catch(this.handleError);

    }



}

Here is the result from this code :

enter image description here

And here is sent headers :

enter image description here

And api :

public function showUserData(Request $request)
{
    $user = $request->header('Authorization');

    return response()->json(self::getAuthenticatedUser($user));
}

I have checked this in the Postman, and everything was okay...:)

F1dzh
  • 74
  • 9
  • Basically, Header is immutable. [Another issue with same problem](https://stackoverflow.com/questions/45286764/adding-a-http-header-to-the-angular-httpclient-doesnt-send-the-header-why) – Akostha Jun 09 '21 at 13:40

1 Answers1

0

I tried with little changes and it s working for me. Lets try below approach.

import { Injectable } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/toPromise';

import { User } from '../../models/user';

@Injectable()

export class UserService {

    public apiUrl: string = 'http://127.0.0.1:8000/api'; // without a slash
    public userData: any = localStorage.getItem('currentUser');
    public token:any = JSON.parse(this.userData);
    public result: any;
    private headers: any;
    private request: any;
    private handleError:any = 0;

    constructor( private http: Http, private route: ActivatedRoute ) {}


    getUserData():Promise<Array<User>>{


        private globalHeaders: Headers = new Headers();     
        globalHeaders.append('Content-Type','application/json');
        globalHeaders.append('Authorization', 'Bearer {'+ this.token.token +'}');


        return this.http.get( this.apiUrl + "/get/active/user-data/",  {headers: this.globalHeaders})
        .toPromise()
        .then(( response ) => {
            console.log(response.json());
            debugger;
            return response.json().data as User[];

        })
        .catch(this.handleError);

    }



}
Pramod Patil
  • 2,704
  • 2
  • 14
  • 20