Rick Kountz
Rick Kountz

Reputation: 41

405 Method Not Allowed POST Laravel

I have a laravel api that I'm trying to run a custom artisan command to process a transaction. The api is suppose to check for pending transactions in our merchant database and post them in our transaction database. I get the following error:

[GuzzleHttp\Exception\ClientException]
  Client error: `POST http://paycentral.mymarket.com/transactions/bulk` resulted in a `405 Method Not Allowed` response:
  {"error":{"message":"405 Method Not Allowed","status_code":405,"debug":{"line":446,"file":"\/var\/www\/vhosts\/maindomai (truncated...)

The API I'm using is located at api.mymarket.com. Searching for errors like this has me believing it's a CORS-related issue. I'm using laravel-cors and added Header set Access-Control-Allow-Origin "*" to the .htaccess in both the public folder for api.mymarket.com and paycentral.mymarket.com. The error is still persisting though. Is there any other possible workaround? We are currently using plesk for our hosting services.

UPDATE: I tried doing a preflight request in the pay subdomain Origin: api.mymarket.com Access-Control-Request-Method: POST Access-Control-Request-Headers: MM

It returned a 500 Internal Error which is progress I guess.

UPDATE Here is the routes.php for paycentral. The cors-library is registered in the app.php.

paycentral routes.php

<?php

$api = app('Dingo\Api\Routing\Router');

// all routes are protected by the Authenticate middleware which makes sure     the client
// is authenticated as *somebody* - each resource is further protected by  the authorization
// policies in the App\Api\V1\Policies files to limit the method calls by which client
// type is attempting to access the resource - these must be mapped in the     AuthServiceProvider
$api->group([
    'version' => 'v1',
    'namespace' => 'App\Api\V1\Controllers',
    'middleware' => 'auth' // use the Authenticate middleware
], function($api) {

/*
     * partial CRUD resource routes
     */

    $api->get('transactions/{id}', 'TransactionController@show');
    $api->post('transactions', 'TransactionController@store');
    $api->put('transactions/{id}', 'TransactionController@update');
    $api->post('transactions/bulk', 'TransactionController@store_bulk');
    $api->post('transactions/get_updates',  'TransactionController@get_updates');

Upvotes: 0

Views: 6741

Answers (2)

Rick Kountz
Rick Kountz

Reputation: 41

I solved the issue. It was an issue with one of the routes not pointing to transactions/bulk. The previous developer made undocumented changes to a couple files without following our version control methods so the production branch was broken.

Upvotes: 1

Ahmad Hajjar
Ahmad Hajjar

Reputation: 1853

Assuming that your route is defined well in the routes.php, and that everything else is fine. Then you may try adding the following line in your filters.php

App::before(function ($request) {
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, PATCH, OPTIONS');
}

Upvotes: 0

Related Questions