John Biggs
John Biggs

Reputation: 53

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!

Upvotes: 2

Views: 6060

Answers (5)

Karim Pazoki
Karim Pazoki

Reputation: 969

You should just add these two headers to your request:

Accept: application/json

Content-type: application/json

Upvotes: 4

John Biggs
John Biggs

Reputation: 53

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!

Upvotes: 0

tompec
tompec

Reputation: 1230

It looks like it dosen't even find the route.

Please replace your api.php file by this and retry a GET request:

<?php

Route::get('recipe', 'RecipeController@getRecipe')->name('get_recipe');
Route::post('recipe', 'RecipeController@postRecipe')->name('post_recipe');

Upvotes: 1

drone6502
drone6502

Reputation: 433

The http status returned is a 404.

The body of the response appears to be an HTML page (404 Error page?)

The server doesn't recognize the URL.

I would check to make sure you are using the correct URL.

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163968

You should turn off Laravel Debugbar when testing API. Or you can just turn it off for all API routes.

Here's a solution for Laravel 5 from here:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\JsonResponse;

class ProfileJsonResponse
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if (
            $response instanceof JsonResponse &&
            app()->bound('debugbar') &&
            app('debugbar')->isEnabled() &&
            is_object($response->getData())
        ) {
            $response->setData($response->getData(true) + [
                '_debugbar' => app('debugbar')->getData(),
            ]);
        }

        return $response;
    }
}

Upvotes: 0

Related Questions