Webhook from External POST

I am trying to get a webhook records from external which they pass the data in POSTFor this I create a Custom File as -

if(isset($_POST)){
	$value = json_encode($_POST);
	executeUpdate("INSERT INTO webhook (type,data) VALUES('WA','".$value."')");
	echo $value;
}
var_dump($_POST);

I got an error message as
Bad Request
The server cannot or will not process the request due to an apparent client error.

Don’t use Custom File as a HTTP request handler, you better read Create Your Own API Action.

I have created the API
the api work find if I hit the URL directly mysite/api/webhookas in the webhook the thirdparty is passing the data through POST
so just try a simple form to pass data through POST

<form action="mysite/api/webhook" method="POST">
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">Last name:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="submit" value="Submit">
</form>

when I click the submit button the page redirect to mysite/api/webhook with an error as
This page isn’t working
If the problem continues, contact the site owner.
HTTP ERROR 401I recheck the url and url correct and working if hit directly in the browser.

I have upgraded to PHPMaker v2024.3Here I am trying to get my webhook at “mysite/api/webhook”
I got the same errorThis page isn’t working
If the problem continues, contact the site owner.
HTTP ERROR 401but If I hit the api directly from the browser it is working

arbei wrote:

Don’t use Custom File as a HTTP request handler, you better read > Create Your Own API Action> .

If you use Custom File, the URL is not “mysite/api/webhook”, it should be the route of your Custom File, read Custom Files → Path → Case 1.If you use API, you need to send the JWT token, read Authenticate User with JWT (JSON Web Token).If you don’t want/need to protect your action with JWT token, you may use Route_Action server event instead.

arbei wrote:

If you use Custom File, the URL is > not > “mysite/api/webhook”, it should be the route of your Custom File, read > Custom Files > → Path → Case 1.

I am not using Custom File. I am creating API under function Api_Action($app){ }arbei wrote:

If you use API, you need to send the JWT token, read > Authenticate User with JWT (JSON Web Token)> .

As I am trying to create a webhook it won’t be possible to sent the JWT tokenarbei wrote:

If you don’t want/need to protect your action with JWT token, you may use > Route_Action > server event.

I have try with the function Route_Action($app){ } also which form a url “mysite/webhook” but it is not working.

thomas wrote:

As I am trying to create a webhook it won’t be possible to sent the JWT token

You should be able to. How did you send the request? You may check if it supports --header. Post the code that you use to send the request.thomas wrote:

I have try with the function Route_Action($app){ } also which form a url “mysite/webhook” but it is not working.


  1. You need to describe how it is “not working”, any error messages? Always post your code for discussion. Make sure your URL to your route action is correct.
  2. You should enable Debug and check errors in the log file.
  3. If you send the request from browser, check the HTTP response in your browser.

In the Form I use the action url as
http://mysite/webhookhere I got the error as
404
Not Found
Route ‘webhook’ not found.when I put the same url
http://mysite/webhook
in the browser address bar it is workingHere I am confuse the same url working in browser address bar and not working on form action url.when I debug the code I got the error log as -

#0 C:\wamp\www\mysite\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(38): Closure->PHPMaker2024\Mysite2024\{closure}(Object(Slim\Http\ServerRequest), Object(Slim\Http\Response), Array)
#1 C:\wamp\www\mysite\vendor\slim\slim\Slim\Routing\Route.php(358): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\ServerRequest), Object(Slim\Http\Response), Array)
#2 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): Slim\Routing\Route->handle(Object(Slim\Http\ServerRequest))
#3 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#4 C:\wamp\www\mysite\vendor\slim\slim\Slim\Routing\Route.php(315): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#5 C:\wamp\www\mysite\vendor\slim\slim\Slim\Routing\RouteRunner.php(68): Slim\Routing\Route->run(Object(Slim\Http\ServerRequest))
#6 C:\wamp\www\mysite\vendor\slim\slim\Slim\Middleware\BodyParsingMiddleware.php(64): Slim\Routing\RouteRunner->handle(Object(Slim\Http\ServerRequest))
#7 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(121): Slim\Middleware\BodyParsingMiddleware->process(Object(Slim\Http\ServerRequest), Object(Slim\Routing\RouteRunner))
#8 C:\wamp\www\mysite\src\CorsMiddleware.php(43): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Http\ServerRequest))
#9 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(121): PHPMaker2024\Mysite2024\CorsMiddleware->process(Object(Slim\Http\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#10 C:\wamp\www\mysite\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(45): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Http\ServerRequest))
#11 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(121): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Http\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#12 C:\wamp\www\mysite\src\LaravelSessionMiddleware.php(45): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Http\ServerRequest))
#13 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(121): PHPMaker2024\Mysite2024\LaravelSessionMiddleware->process(Object(Slim\Http\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#14 C:\wamp\www\mysite\vendor\hkvstore\samesite-cookie\src\SameSiteCookieMiddleware.php(52): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Http\ServerRequest))
#15 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(121): Selective\SameSiteCookie\SameSiteCookieMiddleware->process(Object(Slim\Http\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#16 C:\wamp\www\mysite\vendor\hkvstore\samesite-cookie\src\SameSiteSessionMiddleware.php(52): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Http\ServerRequest))
#17 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(121): Selective\SameSiteCookie\SameSiteSessionMiddleware->process(Object(Slim\Http\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#18 C:\wamp\www\mysite\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(76): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Http\ServerRequest))
#19 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(121): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Http\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#20 C:\wamp\www\mysite\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(Slim\Http\ServerRequest))
#21 C:\wamp\www\mysite\vendor\slim\slim\Slim\App.php(199): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#22 C:\wamp\www\mysite\vendor\slim\slim\Slim\App.php(183): Slim\App->handle(Object(Slim\Http\ServerRequest))
#23 C:\wamp\www\mysite\index.php(150): Slim\App->run()
#24 {main}

thomas wrote:

I have try with the function Route_Action($app){ } also which form a url “mysite/webhook” but it is not working.

arbei wrote:

Always post your code for discussion.

arbei wrote:

If you send the request from browser, > check the HTTP response > in your browser.

Post the Request URL you see in your browser, compare it with the URL you defined in your Route_Action event.Be also reminded that if you have enabled Use route cache:
When you deploy new versions of your scripts (for example, after adding or deleting a table) to production, you must delete the generated file (//cache/RouteCache.php) to ensure that the cache will be re-compiled.

I hope the route_action does not accept POST data
I have created a Route_Action as follows

function Route_Action($app)
{
    $app->get('/webhook', function ($request, $response, $args) {
	$value = json_encode($_POST);
        ExecuteStatement("INSERT INTO webhooktable (cmd) VALUES('$value')");
        return $response->withJson(["message" => "webhook successfully hit."]); // Note: Always return Psr\Http\Message\ResponseInterface object
    });
}

Now I can access the above Route by http://mysite/webhookI call the above route using GET method I use the following code

$url = 'http://mysite/webhook';
$data = array("ONE" => "1","TWO" => "2");
$tmp = ClientUrl($url, $data, "GET");
var_dump($tmp);

The GET method works perfectly.But as I want to accept my webhook for POST data I just change the GET to POSTI did not change the url which is working on GET

$url = 'http://mysite/webhook';
$data = array("ONE" => "1","TWO" => "2");
$tmp = ClientUrl($url, $data, "POST");
var_dump($tmp);

When I sent data through POST I got an error as
{“statusCode”:404,“error”:{“class”:“text-warning”,“type”:“Not Found”,“description”:“Route ‘webhook’ not found.”,“trace”:"#0 C:\wamp\www\mysite\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(38): Closure->PHPMaker2024\Mysite2024\{closure}(Object(Slim\Http\ServerRequest), Object(Slim\Http\Response), Array)…Please kindly help.
Thanks in Advance.

thomas wrote:

$app->> get> (‘/webhook’, function ($request, $response, $args) {

Your code only accept GET.As the docs for Route_Action server event says:
Read Slim Framework’s Routing for details about routing.You should read POST Route.