Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

m11ad's avatar
Level 1

Laravel JSON API - cannot POST with Postman - Error 405 and Error 500 in response

Hi, I made a JSON API with laravel. and I'm trying to test it with POSTMAN before continuing to build a front end for it. My API is a NewsDashboard backend that does the CRUD functions.

Here are the routes:

Route::get('/news', 'App\Http\Controllers\NewsController@index')->name('news.index');
Route::get('/news/{newsItem}', 'App\Http\Controllers\NewsController@show')->name('news.show');
Route::post('/news', 'App\Http\Controllers\NewsController@store')->name('news.store');
Route::patch('/news/{newsItem}', 'App\Http\Controllers\NewsController@update')->name('news.update');
Route::delete('/news/{newsItem}', 'App\Http\Controllers\NewsController@destroy')->name('news.destroy');
Route::get('/news/create', 'App\Http\Controllers\NewsController@create')->name('news.create');
Route::get('/news/{newsItem}/edit', 'App\Http\Controllers\NewsController@edit')->name('news.edit');

The /news showing me and empty array [] , because the DB is empty for now. the postman GET is returning 200 OK too. I tried to post JSON payload using postman :

{
  "title": "Example News Item",
  "content": "This is the content of the news item.",
  "category_id": 1,
  "tags": [1, 2, 3]
}

At first I get the error 419 unknown status ; then I moved my routes from web.php to api.php and the index is /api/news instead of /news.

So I'm testing /api/news

GET http://127.0.0.1:8000/api/news/ returns 200 OK but

POST http://127.0.0.1:8000/api/news/ returns 405 Method Not Allowed

why is this? is there something wrong with my code?

Here is the store() method in the newscontroller:

    public function store(Request $request)
    {
        $request->validate([
            'title' => 'required',
            'body' => 'required',
            'category_id' => 'nullable|exists:categories,id',
            'tags' => 'array',
            'tags.*' => 'exists:tags,id'
        ]);
    
        $newsItem = new NewsItem;
        $newsItem->title = $request->title;
        $newsItem->body = $request->body;
        $newsItem->category_id = $request->category_id;
        $newsItem->url = $request->url;
        $newsItem->save();
    
        $newsItem->tags()->attach($request->tags);
    
        return response()->json($newsItem, 201);
    }

If I remove the validation I get the error 500 instead with the body: 500Internal Server Error Integrity constraint violation: 1048 Column 'title' cannot be null (SQL: insert into `news_items` (`title`, `body`, `category_id`, `url`, `updated_at`, `created_at`) values (?, ?, ?, ?, 2022-12-31 14:52:26, 2022-12-31 14:52:26))

But the thing is that my payload contains the required fields.

I don't know where the problem is originating, somehow the payload is not going to the database.

  • I can post new items directly from database and the App\Model is working fine too because I can post with laravel tinker and save it. But I cannot post using the postman.
0 likes
6 replies
martinbean's avatar

@m11ad You’d be better off:

  1. Using resource routes to register the routes you need instead of registering them manually like that: https://laravel.com/docs/9.x/controllers#api-resource-routes
  2. Writing actual tests instead of using Postman: https://laravel.com/docs/9.x/http-tests#testing-json-apis

You also wouldn’t have routes for things like create and edit if this is an API since these normally return HTML forms; you’d just have the POST store and PUT update routes instead.

1 like
m11ad's avatar
Level 1

@martinbean I followed your advice . removed the redundant routes and also created this test case :


namespace Tests\Feature;

use App\Models\NewsItem;
use Tests\TestCase;

class NewsControllerTest extends TestCase
{

    public function testStore()
    {
        // Given
        $data = [
            'title' => 'Test News Item',
            'body' => 'This is the content of the test news item.',
            'url' => 'http://example.com/test-news-item',
        ];

        // When
        $response = $this->postJson(route('news.store'), $data);
        // Then
        $response->assertStatus(201);
        $this->assertDatabaseHas('news_items', [
            'title' => 'Test News Item',
            'body' => 'This is the content of the test news item.',
            'url' => 'http://example.com/test-news-item',
        ]);
    }
}

The test passes! and the sample payload gets saved in the db too. But when I want to POST the same payload with Postman I get 405 instead of 201 and the body has this error :

MethodNotAllowedHttpException: The / method is not supported for route POST. Supported methods: GET, HEAD. in file 

Still issue is not resolved and I don't know what the root cause is.

martinbean's avatar

@m11ad Your error message makes no sense. It’s saying you're trying to use a “/” method, on a route named “POST”. So show the full error.

eightyfive's avatar
Level 1

You need to set Accept: application/json header in Postman.

In my case I was manually throwing a ValidationException in my controller, but because this header was not set on the request, somehow Laravel lost track of the 422 and made it 405 HTTP error...

(The hint was that the application Handler was returning HTML instead of JSON in Postman)

2 likes
m11ad's avatar
Level 1

@eightyfive Wow, that was the answer; thank you. I can't believe it. I thought something was wrong with my code. but it was something wrong with Postman default config!

1 like

Please or to participate in this conversation.