1

I need to use my API to delete some entities, I create my controller, my methods, the routes. They works fine, all the get and put/patch method works, but with the delete one I have and error throw by my Angular app who consume this api, here is the error :

DELETE (Method Not Allowed)

In my api route file I set this :

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, PATCH, DELETE');
header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization, X-HTTP-Method-Override');

Anyone know why it's not working ?

Edit:

Here is my routes (and yes the methods are in the good controller) :

enter image description here

Spialdor
  • 1,475
  • 5
  • 20
  • 46
  • 1
    Do you have route for delete method? – FULL STACK DEV Jul 18 '18 at 08:07
  • I've created my routes with the Route::resource method, and yes I have a route for the delete method (I can see the routes with php:artisan route:list) – Spialdor Jul 18 '18 at 08:08
  • Are you sure, you are issuing the request against the correct `URI`? –  Jul 18 '18 at 08:09
  • Yes i'm sure, I can see the url requested and it's the good one, the same url with postman works fine. But with angular Laravel throw a MethodNotAllowedHttpException – Spialdor Jul 18 '18 at 08:10

3 Answers3

3

When you are posting to the Delete url, make sure you have this data in your post request:

_method=delete

this is just like an input field e.g:

<input type="hidden" name="_method" value="delete">
Petar Vasilev
  • 4,281
  • 5
  • 40
  • 74
  • And how can I add this ? I'm using restangular to do the requests – Spialdor Jul 18 '18 at 08:14
  • I try to add this : `.customDELETE(undefined, undefined, undefined, {_method: 'delete'});` and `.customDELETE(undefined, undefined, undefined, '_method=delete');` but still the same error – Spialdor Jul 18 '18 at 08:21
  • Check this out: https://stackoverflow.com/questions/18585010/restangular-post-form-data-in-json-format-in-angular-js – Petar Vasilev Jul 18 '18 at 08:24
  • That's what i'm doing : `this.restangular.one(this.route, resource.uuid).customDELETE(undefined, undefined, undefined, '_method=delete');` – Spialdor Jul 18 '18 at 08:27
  • You need to do something like this: Restangular.all('data.json/:user').post("ul-to-post-to", $scope.data); – Petar Vasilev Jul 18 '18 at 08:30
  • In the documentation it's like this : `Restangular.one("accounts", 123).remove();`, i'm not gonna do a post to make a delete request – Spialdor Jul 18 '18 at 08:33
  • Just try it to see if that's the problem then you can think of another solution. – Petar Vasilev Jul 18 '18 at 08:36
1

Method not allowed is an HTTP Status code 405, and it usually translates to the lack of HTTP Verb matching that endpoint

Edit: Also, check this

e.g.

All of these SHOULD mean different things and DO different things. If you make a Request to the last url but the url isn't registered (on your routes files or wherever you place them), then that's the error it returns because it matches the name but not the verb

POST   url.com/user

GET    url.com/user

PUT    url.com/user

PATCH  url.com/user

DELETE url.com/user
abr
  • 2,071
  • 22
  • 38
0

try this package CORS Middleware for Laravel 5

Mike Foxtech
  • 1,633
  • 1
  • 6
  • 7