Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch patch request is not allowed

I have two apps one is a react front end and the second one is the rails-api app.

I have been happily using isomorphic-fetch till I needed to send PATCH method to the server.

I am getting:

Fetch API cannot load http://localhost:3000/api/v1/tasks. Method patch is not allowed by Access-Control-Allow-Methods in preflight response. 

but the OPTIONS response from the server includes a PATCH method in a list of Access-Control-Allow-Methods:

enter image description here

This is how the fetch is implemented:

const API_URL = 'http://localhost:3000/'                                             const API_PATH = 'api/v1/'  fetch(API_URL + API_PATH + 'tasks', {   headers: {     'Accept': 'application/json',     'Content-Type': 'application/json'   },   method: 'patch',                                                                 body: JSON.stringify( { task: task } )                                         }) 

POST, GET, DELETE are set up pretty much the same and they are working fine.

Any idea what is going on here?

UPDATE:

Method patch is case sensitive:

https://github.com/github/fetch/blob/master/fetch.js#L200

Not to sure if this is intended or a bug.

UPDATE 2

This is intended and the method type PATCH needs to be case sensitive. Updating the line from fetch method to:

method: 'PATCH' 

fixes the problem.

https://github.com/github/fetch/issues/254

like image 825
Kocur4d Avatar asked Jan 07 '16 22:01

Kocur4d


People also ask

How do you send a Fetch PATCH request?

To update a resource with the Fetch API is very simple and straightforward, all you have to pass in is the URL of the endpoint as the 1st parameter and an object which contains the details of the method, headers, and body as the 2nd parameter.

What is a fetch error?

The fetch() function will automatically throw an error for network errors but not for HTTP errors such as 4xx or 5xx responses. For HTTP errors we can check the response. ok property to see if the request failed and reject the promise ourselves by calling return Promise.

How do I send a PATCH request to API?

To send a PATCH request to the server, you need to use the HTTP PATCH method and include the request data in the body of the HTTP message. The Content-Type request header must indicate the data type in the body. In this PATCH request example, we send JSON to the ReqBin echo endpoint to update the data on the server.

What is fetch no CORS mode?

Note that mode: "no-cors" only allows a limited set of headers in the request: Accept. Accept-Language. Content-Language. Content-Type with a value of application/x-www-form-urlencoded , multipart/form-data , or text/plain.


1 Answers

I had a very similar problem with reactJS front end and rails API using Rack::Cors, and adding patch to the list of allowed methods solved the problem for me.

config.middleware.insert_before 0, Rack::Cors do   allow do     origins '*'     resource '*', headers: :any, methods: [:get, :post, :patch, :options]   end end 
like image 67
DannyRosenblatt Avatar answered Sep 17 '22 05:09

DannyRosenblatt