Error using POST in Laravel API

3

I'm learning how to build an API using Laravel 5.4. *.

No Api.php :

$this->get('products', 'API\ProductController@index', ['except' => [
    'create', 'edit'
]]);

No Product.php :

class Product extends Model
{
    protected $fillable = ['name', 'description'];
}

No ProductController.php :

private $product;

public function __construct(Product $product)
{
    $this->product = $product;
}

public function index()
{
    $products = $this->product->all();

    return response()->json(['data' => $products]);
}

public function store(Request $request)
{
    return response()->json([
        'result' => $this->product->create($request->all())
    ]);
}

I reviewed my code and could not find any errors.

When I use GET it returns the products registered, however when I use POST I get this error:

What can it be?

    
asked by anonymous 13.10.2017 / 21:00

1 answer

3

Your code is correct and the error is also:)

$this->get('products'

This route specifies that only requests through the get method will be processed otherwise it will return the code 405 method not allowed. Conceptually research is done through the get method, to create / add new features the post is used.

Recommended reading:

What are the advantages of using the correct HTTP methods

What is REST and RESTful?

REST and HTTP are the same?

Rest api tutorial

Original article on rest

API design - MS Guide

    
13.10.2017 / 21:06