POST request arrives as GET, returns 405: Method Not Allowed

Posted 3 years ago by mark.p.ramos

I've defined an endpoint to receive POST requests from SendGrid's Parse API. My issue is that many of these requests return "405: Method Not Allowed" to SendGrid.

If I open the endpoint to all request methods, it never generates 405 errors. However by the time execution reaches my controller method $request->method() often returns 'GET' and all of the expected POST parameters are missing.

  1. I have confirmed with SendGrid they ONLY send POST requests
  2. I have setup a RunScope endpoint to capture all their requests and independently confirmed they ONLY send POST requests
  3. Below is a recent stretch from my access log
167.89.125.253 - - [14/Aug/2016:14:57:34 -0400] "POST /endpoints/sendgrid/mail/email HTTP/1.1" 405 4485 "-" "SendGrid 1.0"
167.89.125.221 - - [14/Aug/2016:14:58:01 -0400] "POST /endpoints/sendgrid/mail/email HTTP/1.1" 405 4485 "-" "SendGrid 1.0"
167.89.125.235 - - [14/Aug/2016:15:06:04 -0400] "POST /endpoints/sendgrid/mail/email HTTP/1.1" 405 4485 "-" "SendGrid 1.0"
167.89.125.254 - - [14/Aug/2016:15:14:21 -0400] "POST /endpoints/sendgrid/mail/email HTTP/1.1" 204 - "-" "SendGrid 1.0"
167.89.125.225 - - [14/Aug/2016:15:21:14 -0400] "POST /endpoints/sendgrid/mail/email HTTP/1.1" 405 4485 "-" "SendGrid 1.0"

The access log also indicates that all requests are POST. I don't see any redirects happening. And while sometimes the requests work as expected, most of the time they result in 405.

I am losing my mind over this one. Please help!

My route definition:

Route::group(['prefix' => 'endpoints', 'namespace' => 'Endpoints'], function() {
    Route::group(['prefix' => 'sendgrid'], function() {
            Route::group(['prefix' => 'mail'], function() {
                Route::post('email', ['uses' => '[email protected]', 'as' => 'sendGrid.mail.email']);
         });
    });

    Route::group(['prefix' => 'leads'], function() {
            Route::post('email', ['uses' => '[email protected]', 'as' => 'sendGrid.leads.email']);
            Route::post('prospects', ['uses' => '[email protected]', 'as' => 'sendGrid.leads.prospects']);
        });
});

My controller:

namespace App\Http\Controllers\Endpoints;

use Exception;
use Log;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Services\SendGrid\ProxyEmailManager;
use App\Services\SendGrid\Email;
use App\Services\SendGrid\UserNotFoundException;
use App\Services\SendGrid\ProspectNotFoundException;
use App\Utility\StringUtility;


class SendGridController extends Controller {
    
    public function incomingEmail(Request $request) {
        try {
            $email = Email::fromRequest($request);
            if (StringUtility::emptyOrWhitespace($email->body())) {
                return response('email is empty', 202);
            }

            $proxyEmailManager = new ProxyEmailManager();
            if ($proxyEmailManager->isUserProxyAddress($email->to)) {
                return $this->forwardToUser($email, $proxyEmailManager);
            }

            if ($proxyEmailManager->isProspectProxyAddress($email->to)) {
                return $this->forwardToProspect($email, $proxyEmailManager);
            }
        } catch (Exception $ex) {
            Log::error($ex->getMessage(), ['stack trace' => $ex->getTraceAsString()]);
            return response($ex->getMessage(), 202);
        }
        
        return response('unrecognized proxy address', 202);
    }
}

Please sign in or create an account to participate in this conversation.