I've Created a new Laravel 5.4 Project, Created the model ' Recipe ' , the migrations table ' recipes ' and added the routes to the ' Api.php ' . But when testing my API in Postman, instead of getting a Json Response i get a Html File.
Here are the codes as per the below files:
API.php Routes file
<?php
use Illuminate\Http\Request;
Route::post('/recipe', 'RecipeController@postRecipe')-
>name('get_recipe');
Route::get('/recipe', 'RecipeController@getRecipe')-
>name('post_recipe');
RecipeController
namespace App\Http\Controllers;
use App\Recipe;
use Illuminate\Http\Request;
class RecipeController extends Controller {
public function postRecipe(Request $request)
{
$recipe = new Recipe();
$recipe->content = $request->input('content');
$recipe->save();
return response()->json(['recipe' => $recipe], 201);
}
public function getRecipe()
{
return response()->json(['message' => 'Got a Response'],200);
}
Recipe Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Recipe extends Model
{
protected $fillable = ['content'];
}
The migration was migrated and the table ' recipes ' created ' .
Then in postman i try a ' get ' request to my url: ' recipeapi.dev/api/recipe ' but get html codes.
The same happens when i try a ' post ' request to my url: ' recipeapi.dev/api/recipe ' . OFC for the post request i include headers: ' Content-Type ' ' application/json ' . Still getting the same html codes....
Using Homestead/vagrant for laravel and if i use browser to go to default route: '/' it brings me to the welcome Laravel Page.
Don't know whats the problem i keep getting these html codes and not Json Data as per my controller.
Here are the pics of postman with the data....
Anyone got any idea what's going on? Why are my not getting Json data from my api from the get and post request?
Thanks Guys!