Can't access post route from API callback - php

I use a webhook to receive callback post from an API server. I've tested to post data from postman, it's works fine but from API server i can't receive anything.
But when i changed webhook by using webhook.site, it's work, but with my domain webhook its not, didn't even access my post route.
My route:
Route::group(['namespace' => 'Web'], function () {
//Callback card
Route::post('/callback', 'CustomersController#callbackCard');
My Controller:(If accessed post route will store a file)
public function callbackCard(Request $request){
Storage::put('Accessed.txt', '1');
}
Sorry for my bad English
Can anyboy help me.

the callback API just send Post with application/x-www-form-urlencoded;charset=utf-8
my url: domain/callback
default namespace Web

Related

Slim Framework: Method not allowed Method not allowed. Must be one of: POST

I'm setting up a REST-server in PHP and want to allow the client to use an endpoint with different methods like GET, POST, PUT, DELETE, ...
But there is a problem when I try adding the function for the POST method: The application runs the function for GET if I try to access it with POST via Postman.
I already tried to comment the GET function but if I do this, I get an error 405.
// Just a testing function for POST
$app->post('/users', function (Request $request, Response $response, array $args)
{
$user = $request->getParsedBody();
$response->getBody()->write(json_encode($user->getWrapperClass()));
return $response->withHeader('Content-Type', 'application/json');
});
Anyone can help me?
I have found the problem: It was not Postman and not my code. The problem was the URL entered in Postman: It was a http URL and the server has an automatic redirection to https. During this process the HTTP method just changed to GET instead of POST, PUT or anything else... Now changed the URL to https: It works fine now!

How to log requests when using Silex JWT authentication call and return custom message for invalid token

I'm using Silex framework + JWT token
Sample code is as follows, Example from this link
$app->get('/api/protected_resource', function() use ($app){
// Loggger
});
Everything is working fine.
If I call /api/protected_resource without jwt token it gives error as
{"message":"A Token was not found in the TokenStorage."}
Here I want to log the every request [with or without token] and send Custom message for invalid token.
I tried using $app->before(), but for invalid calls this function didn't executed.
So how to add logs for evey calls and Is there any way to configure the Custom message directly ?
You can listen to the security.authentication.failure event. Sample code:
<?php
// somewhere before calling $app->run();
$app->on("security.authentication.failure", function() use $app {
$app['logger']->log("Authentication failure!")
});
This is according to the Symfony security docs.

Implement Gocardless webhook in Laravel

I was trying to implement a webhook in laravel.
I have created access token and created webhook endpoint also.
my webhook end point is like,https://www.example.com/gocardless.php
and my route is like,
Route::get('/gocardless.php',
'\App\Http\Controllers\gocardlessController#remote')->name('remote');
Controller code like,
class gocardlessController extends Controller
{
public function remote(Request $request)
{
$token ="token";
$raw_payload = file_get_contents('php://input');
$headers = getallheaders();
$provided_signature = $headers["Webhook-Signature"];
$calculated_signature = hash_hmac("sha256",$raw_payload,$token);
if ($provided_signature == $calculated_signature) {
$payload = json_decode($raw_payload, true);
}
}
}
But when i clik on send test webhook in gocardless account,they are given "405 no method found" as responce.
How i can solve this?
The HTTP 405 error you're seeing indicates that your Laravel application doesn't know how to handle the method of the incoming request.
GoCardless webhooks use the POST method to send you a request with a JSON body, but the route you've written is for handling a GET request (Route::get). To resolve this, you should define a route for POST requests to the endpoint which will receive webhooks.
A few remarks and fixes
Remark
Why do you include the "ugly" .php extension in your route, there is no need for that
Fix
Change your route (in web.php) to
Route::get('gocardless', 'gocardlessController#remote');
Remark
I also see you start your controller name with lowercase, this is not common practise
Fix
Don't forget to add these lines in your controller at the top
namespace App\Http\Controllers; // declare right namespace
use Illuminate\Http\Request; // Hint which Request class to use below
For the body: that you really have to write yourself and return the data as json for example

Wordpress REST API custom endpoint for method POST

I am currently working on a project which requires a WordPress website and a simple REST api. I discovered that WordPress has its own REST api and decided to extend its functionality to meet my needs. All I need to do is to have endpoints for GET and POST requests that retrieve/insert data from/to a table which is not directly related to WordPress (but in the same database). I successfully implemented all GET requests, however, I am struggling to get the POST ones working.
I have this route register defined:
register_rest_route('api/v1', 'create-player/', array(
'methods' => 'POST',
'callback' => 'create_player',
));
The client sends a request through ajax call which is expected to hit the endpoint from the route above. This is the ajax:
$.ajax({
method: "POST",
url: '/wp-json/api/v1/create-player/',
data : JSON.stringify(data),
contentType: 'applcation/json',
beforeSend: function (xhr){
xhr.setRequestHeader("X-WP-None", locData.nonce);
console.log('beforeSend');
},
success: function(response){
console.log("success" + response);
},
fail: function (response){
console.log("fail" + response);
}
});
I am not sure how to build the POST route register from the REST api, the other GET requests have an attribute args that map directly to the parameters passed in the endpoint. Do I need something like that to handle the request data when using POST? How do I get the data type passed from the ajax and then use it in my function create_player(); The WP REST API documentation seems to be incomplete and all of the information I found uses endpoints for built-in WordPress features such as posts/authors/blogs etc. but I don't need that, I just want to use the provided functionality and create my own interface. Thank you.
in your callback function you can use something like this:
$param = $request->get_param( 'some_param' );
// You can get the combined, merged set of parameters:
$parameters = $request->get_params();
https://www.coditty.com/code/wordpress-api-custom-route-access-post-parameters
Finally found it! In order to access the body of the POST request use $request->get_body(); in your register_rest_route callback method.
Add POST in methods while registering route and in your callback function access your POST variables via $request array. That's it.

PHP : Post routes not working, 404 not found

I'm using Slim framework. I've made an API with Post routes and Get routes
The Get ones are working perfectly
The Post ones are not.
this one is working when accessed via javascript or php
$app->get('/test',function(){
});
While this one return an error 404 not found when accessed
$app->post('/testpost',function(){
});
I can't figure out the problem
thank you for your help
Read the docs.
POST Route
You can add a route that handles only POST HTTP requests with the Slim application’s post() method. It accepts two arguments:
The route pattern (with optional named placeholders)
The route callback
$app = new \Slim\App();
$app->post('/books', function ($request, $response, $args) {
// Create new book
});
If you are posting your data and don't see it, that's because you're not passing any $request parameter to the callback.
The Slim's router is based on nikic/FastRoute, so if you prefer you may also refer to its docs to better understand it.
How are you testing?
Start up the PHP built in web server via php -S
and then I recommend using Curl:
curl -v -X POST http://localhost:8080/testform

Categories