0

My controller is not returning the request body from the POST request. I am trying to make a login in Ionic that will direct to my laravel server.

Laravel : Controller

public function store(Request $request){
     return $request->all();
}

Ionic : register.ts (src: http://www.nikola-breznjak.com/blog/javascript/ionic3/posting-data-ionic-3-app-php-server/)

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http'; //https://stackoverflow.com/questions/43609853/angular-4-and-ionic-3-no-provider-for-http

@Component({
 selector: 'page-register',
 templateUrl: 'register.html'
})

export class RegisterPage {
 data:any = {};

 constructor(public navCtrl: NavController, public http: Http) {
 this.data.username = '';
 this.data.response = '';

 this.http = http;
 }

 submit() {
 var link = 'http://127.0.0.1:777/api/user';
 var myData = JSON.stringify({username: this.data.username});

 this.http.post(link, myData)
 .subscribe(data => {
 this.data.response = data["_body"]; //https://stackoverflow.com/questions/39574305/property-body-does-not-exist-on-type-response
 }, error => {
 console.log("Oooops!");
 });
 }
}

So I investigated it, and I notice the payload is in JSON like this: Request Payload.

But when I try to use json_decode() for it, the Laravel Validation won't work.

2 Answers2

0

I found an answer!

Instead of using the laravel's default auth validator.

public function store(Request $request)
{
    // return $request->all();

    $payload = json_decode(request()->getContent(), true);

    $this->validator($payload);

}

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'username' => 'required|string|username|max:50|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }

So I look for a way that will validate a JSON object instead of a Request body and I found this method: Laravel: validate json object

Therefore I updated my code to like this:

public function store(Request $request)
    {
        // return $request->all();

        $payload = json_decode(request()->getContent(), true);
        $data = $payload;

        $rules = [
                'username' => 'min:5'
            ];

            $validator = Validator::make($payload, $rules);
            if ($validator->passes()) {
                return 'success';
            } else {
                //TODO Handle your error
                return $validator->errors()->all();
            }
}
0

You have to use new FormData() method

import 'rxjs/add/operator/map'; //FOR MAP DATA INTO JSON OR TEXT
import 'rxjs/add/operator/timeout';  //FOR TIMEOUT
import { Http,Headers,RequestOptions} from '@angular/http';  

passdata(){
  this.headers = new Headers({ 'Content-Type': 'application/json' });  
  let body = new FormData();
  body.append("paramsmane","paramsvalue") 
  console.info(body);   
  return new Promise((resolve, reject) => {   
  return this.http.post('url', body,  this.headers)
  .timeout(40000)
  .map(response => response.json()).subscribe(res => { 
    console.info(res);
  },(reject) => {
    console.info('error',reject);
  })
})
}
Manoj Bhardwaj
  • 736
  • 1
  • 9
  • 25