Rox
Rox

Reputation: 361

Postman and laravel delete api

I am trying to make laravel api, getting request is all working fine but when I use delete request, it show this image

Please help , what can I do for this.

And this is in my web.php,

Route::delete('article/{id}','ArticleController@destroy');

And in ArticleController

public function destroy($id)
{
    $article=Article::FindOrFail($id);
    if($article->delete()){
        return new ArticleResources($article);
    }
}

Upvotes: 3

Views: 4832

Answers (2)

Sogeking
Sogeking

Reputation: 830

In addition to @joakim-lien answer, you'll face another problem using Postman.

HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method

source: Form Method Spoofing - Laravel docs

This is only related to HTML forms and Postman requests, so, if you want to make a DELETE request, you'll need to do a POST request and add a "_method" field set to "DELETE"

enter image description here

Upvotes: 5

Joakim Lien
Joakim Lien

Reputation: 36

API routes should be placed in the api.php and not web.php

The problem with placing API routes within web.php is that those routes uses web middleware that includes stuff like CSRF protection.

Your postman image doesn't really help, but I can see you get the "Page expired" title, and I assume the problem is CSRF.

So the easiest fix to this problem is to place your API routes in api.php.
Routes here is prefixed with 'api/' so in this case the new url would be something like:

DELETE | http://127.0.0.1:8000/api/article/2

Upvotes: 2

Related Questions