laravelpostman

Laravel 5.4 API not returning JSON in Postman


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....

Get request with Postman

Post request with Postman

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!


Solution

  • I appreciate all your help! You guys are very kind to take time away from your busy schedules.

    I finally solved this issue. I feel very silly now. I just had to do a vagrant command ' vagrant provision ' in the homestead directory since i'm using Homestead, and all was working.

    I seem to get this issue from time to time especially when i create Authorisations ' php artisan make:auth ' , in which i would thereafter have to make a vagrant provision command in order for the login, register parts ect.. of authorisations to show up on webpages.

    Don't know what's the cause of this issue, but problem solved.

    Thanks again Guys! Appreciate it!