The exact same query returns 200 to the mail worker, but 404 for the React application

Using the Laravel resource routes, I configured the API to serve as a back-end React JS application. I am trying to access the "update" method currently. I use Javascript fetch()to accomplish this, so first I need to make one request OPTIONSand then make a request POST(the form has a spoof method, instead of _methodto PATCH- this obviously does not affect the initial call OPTIONS). The same page also queries GETthe same endpoint using the same method that works just fine.

Below is the challenge fetch(). Of course, this is React, he called the Redux Saga process, but there is an actual selection.

function postApi(values, endpoint, token) { // <-- values and endpoint are sent by the component, token is sent by a previous Saga function
    return fetch(apiUrl + endpoint, { // <-- apiUrl is defined as a constant earlier in the file
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + token
        },
        body: JSON.stringify(
            values
        )
    }).then(handleApiErrors)
      .then(response => response.json())
      .catch((error) => {throw error})
}

And the Laravel routes:

Route::group(['middleware' => 'auth:api'], function() {
    Route::resource('users', 'UserController');
}

, OPTIONS URL- 404, , , , , , , , , , Laravel , . , , , Postman. , Postman.

( , ):

Access-Control-Allow-Origin:*
Cache-Control:no-cache, private
Connection:close
Content-Length:10
Content-Type:text/html; charset=UTF-8
Date:Thu, 21 Sep 2017 13:29:08 GMT
Server:Apache/2.4.27 (Unix) OpenSSL/1.0.2l PHP/7.0.22 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By:PHP/7.0.22

, React JS (, 404):

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8,fr;q=0.6,ga;q=0.4
Access-Control-Request-Headers:authorization,content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:localhost
Origin:http://localhost:3000
Pragma:no-cache
Referer:http://localhost:3000/employees/edit/13
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36

Postman OPTIONS . ! 200.

, Apache. :

...
::1 - - [20/Sep/2017:15:33:24 -0400] "OPTIONS /the/path/to/api/users/13 HTTP/1.1" 200 -
::1 - - [20/Sep/2017:15:40:26 -0400] "OPTIONS /the/path/to/api/users/13 HTTP/1.1" 404 10
...

, URL , 200, 404, - .

, , POST , create .

?


1. (React Native + fetch + API: DELETE , Postman) , Apache, Nginx, , - URL- . OPTIONS 301 ( ).

2. . :

Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@create');
Route::put('/users/{user}', 'UserController@update');
Route::patch('/users/{user}', 'UserController@update');
Route::get('/users/{user}', 'UserController@show');

Postman 200 OK, React - 404 Not Found.

3. ! . Chrome cURL Postman - , - . , , 404!

/ , Origin Access-Control-Request-Method. , 200, , 404.

However, this still leaves me with a question on how to fix this problem. At this point, I am wondering if the question could become a more important question of Laravel - IE, why a request OPTIONSfor a completely correct resource path will return 404. I assume that these resource paths are listening PUTor PATCHnot OPTIONS.
+4
source share

Source: https://habr.com/ru/post/1686070/


All Articles