Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to send a DELETE request with fetch api

When I send a delete request to a certain endpoint, for example with httpie from terminal like

http delete http://localhost:8181/admin/applications/uspecs

I get a valid behavior, as in { success: true } as response body. But when I do

fetch (
  'http://localhost:8181/admin/applications/uspecs',
  { method: 'DELETE' }
)
.then(res => doSomethingWithResponse())
.catch(err => console.error(err))

In javascript code, then I get a

Fetch API cannot load http://localhost:8181/admin/applications/uspecs.
Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response.

error on the console. What am I missing? I am getting a valid methods list on options request.

like image 930
nabn Avatar asked Feb 22 '16 08:02

nabn


People also ask

How do I use Fetch to send a request?

Fetch also supports the POST method call. To do a POST request we need to specify additional parameters with the request such as method, headers, etc. In this example, we'll do a POST request on the same JSONPlaceholder and add a post in the posts. It'll then return the same post content with an ID.

Does fetch send a GET request?

The fetch() method: Fetch API comes with a fetch () method that allows you to fetch data from all sorts of different places and work with the data fetched. It allows you to make an HTTP request, i.e., either a GET request (for getting data) or POST request (for posting data).


1 Answers

You need to send the Access-Control-Allow-Methods header containing the allowed methods. Currently your header is named methods.

like image 181
ThiefMaster Avatar answered Sep 30 '22 21:09

ThiefMaster