cac
2974
2
Laravel

Laravel 5.5 Guzzle 6.3 PUT and POST request to API

Posted 1 year ago by cac

I am having a hard time making those 2 request. I built and api and everything is working fine on POSTMAN. All endpoints are behaving as expected. But on my client I can't seem to build those two requests properly. I have tried everything I could think of. Here is my api code:

API CONTROLLER

<?php

namespace App\Http\Controllers;

use App\Http\Requests\StorePerson;
use App\Http\Requests\UpdatePerson;
use App\Http\Resources\PersonResource;
use App\Person;
use Repositories\PersonRepository as ICrud;
use Symfony\Component\HttpFoundation\Response;

class ApiPersonController extends Controller
{
    protected $persons; //Repository

    public function __construct(ICrud $p)
    {
        $this->persons = $p;
    }

    public function index()
    {
        try {
            return json_encode($this->persons->all());
        } catch (\Exception $e) {

            return response(null, Response::HTTP_INTERNAL_SERVER_ERROR);

        }

    }

    public function store(StorePerson $request)
    {
        try {
            $return = $this->persons->store($request);
            if (isset($return)) {
                return response(['data' => $return], Response::HTTP_CREATED);
            } else {
                return response(null, Response::HTTP_BAD_REQUEST);
            }
        } catch (\Exception $e) {
            return response(null, Response::HTTP_INTERNAL_SERVER_ERROR);

        }


    }
 public function update(UpdatePerson $request, Person $person)
    {
        try {
            $return = $this->persons->update($request, $person);
            if (isset($return)) {
                return response(['data' => $return], Response::HTTP_CREATED);
            } else {
                return response(null, Response::HTTP_BAD_REQUEST);
            }
        } catch (\Exception $e) {
            return response(null, Response::HTTP_INTERNAL_SERVER_ERROR);
        }


    }
}

REPOSITORY

<?php

namespace Repositories;

use App\Http\Resources\PersonCollection;
use App\Http\Resources\PersonResource;
use App\Person;
use Interfaces\iCrud; //adicionei caminho da pasta interface no composer.json e dei comando <composer dump-autoload> no cmd


class PersonRepository implements iCrud
{

    public function all()
    {
        return PersonCollection::collection(Person::all());
    }

    public function store($r)
    {
        $person = new Person();
        $person->name = $r->nome;
        $person->ssn = $r->cpf;
        $person->gender = $r->genero;
        $person->address = $r->endereco;
        $person->birthday = $r->nascimento;
        $person->save();
        return new PersonResource($person);
    }

    public function update($r, $object)
    {


        $r['name'] = $r->nome;
        $r['ssn'] = $r->cpf;
        $r['gender'] = $r->genero;
        $r['address'] = $r->endereco;
        $r['birthday'] = $r->nascimento;
        unset($r['nome']);
        unset($r['cpf']);
        unset($r['genero']);
        unset($r['endereco']);
        unset($r['nascimento']);
        $object->fill($r->all());
        $object->save();

        return new PersonResource($object);
    }
}

CLIENT CONTROLLER

<?php

namespace App\Http\Controllers;


use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Http\Request;

class ClientPersonController extends Controller
{
    public function index()
    {
        $client = new Client();
        $res = $client->get(route('persons.index'));
        //echo $res->getBody();
        $persons = json_decode($res->getBody()->getContents());


        return view('persons.index', compact('persons'));
    }

   
    public function create()
    {
        return view('persons.create');
    }

    public function store(Request $r)
    {
        unset($r['_method']);
        unset($r['_token']);
        $endpoint = url('/') . '/api/persons/';
        dd($endpoint);
        $headers = ['Content-Type' => 'application/x-www-form-urlencoded',
            'Accept' => 'application/json'];
        $body = json_encode($r->all());
        $options = [$headers, $body];
        $client = new Client();
        dd($client->post($endpoint, $options));
    }

 
    public function edit(Request $r)
    {

        $client = new Client();
        $res = $client->get($r->href);
        $person = json_decode($res->getBody()->getContents());

        return view('persons.edit', compact('person'));
    }

    public function update(Request $r)
    {
        unset($r['_method']);
        unset($r['_token']);
        $endpoint = url('/') . '/api/persons/' . $r->id;
        $body = json_encode($r->all());
        $client = new Client();
        $res = $client->put($endpoint, ['body' => json_encode($r->all())]);    
        return redirect()->action('[email protected]');


    }

    public function destroy(Request $r)
    {

        $client = new \GuzzleHttp\Client();
        $client->delete($r->href);

        return redirect()->action('[email protected]');

    }
}

Any thoughts on this?

Please sign in or create an account to participate in this conversation.