Friday, March 3, 2017

Laravel API doesn't recognise data sent with Angular HTTP Post

I am working on a Laravel API and Angular. It seems that when I make an HTTP post with Angular, even if the data are on the request, the API response is that the related fields are required. However, exactly the same request from Postman works.

This is my component:

export class Login {

  user: User;
  data: any;

  constructor(private _loginService: LoginService) {}

  onLogin() {
    let userInput = {'email': 't@a.com', 'password': '123456'};
    this._loginService.getToken(userInput)
        .subscribe(v => {
          this.data = v;
        });
      return;
    }
}

and here is my provider:

@Injectable()
export class LoginService {

  constructor(
      private _http: Http,
      private _config: AppConfig
    ) {}

  API = this._config.config.apiUrl;

  private headers = new Headers({ 'Content-Type': 'application/json' });

  getToken(user: User) {
    let body = JSON.stringify(user);

    return this._http
      .post(`${this.API}/auth/login`, body, this.headers)
      .map(res => res.json().data)

  }

}

While inspecting the network tab on the Developer console, the data seems to have passed on the request:

Request Payload
{"email":"t@a.com","password":"123456"}

As I said, Postman works with the same data. So, I guess the issue is on Angular and not Laravel.



via Tasos

Advertisement