Reputation:
Trying to simulate a POST request on postman for a callback function for API. It is from an external API posting the data to me so I did not include return in callback function.
Here is my route:
Route::post('api-callback','APIController@callback')->name('api-callback');
Controller:
public function callback(Request $request){
Log::info('callback function is called');
$response = request()->all();
Log::info($response);
if($request->status == 1){
//store transaction details
$transaction = new Transaction();
$transaction->user_id = auth()->id();
$transaction->billcode = $request->billcode;
$transaction->transaction_id = $request->refno;
$transaction->amount = $request->amount;
$transaction->save();
//update new credit balance for user
$user_credit = UserInfo::where('user_id',auth()->id());
$user_credit->credit += $request->amount;
$user_credit->save();
}
The expected result is to return 1(success). I tested it few days ago and it works as expected but I tried it again today and it is returning me HTML text with no errors. The status is 200 as well. Screenshot just for reference.
How should I debug this?
Upvotes: 2
Views: 5184
Reputation: 84
you can use this code
public function callback(Request $request){
Log::info('callback function is called');
$response = request()->all();
Log::info($response);
if($request->status == 1){
//store transaction details
$transaction = new Transaction();
$transaction->user_id = auth()->id();
$transaction->billcode = $request->billcode;
$transaction->transaction_id = $request->refno;
$transaction->amount = $request->amount;
$transaction->save();
//update new credit balance for user
$user_credit = UserInfo::where('user_id',auth()->id());
$user_credit->credit += $request->amount;
$user_credit->save();
return response()->json(['status' => 'success'], 201);
}
Upvotes: 0
Reputation: 1349
The problem is that you are sending a form-data and have forgotten to add Accept: Application/json
in your header. If you do that, laravel will automatically return all responses in json
.
You can either send form-data with that header, or in postman use raw body with type json and the postman will add that header for you.
Also in your function, you have no return at the end, like:
return response()->json([
"status" => 200,
"message" => "Success",
"data" => $transaction
]);
Upvotes: 1
Reputation: 2957
By default, Laravel return html formatted response - for json - client must set Header Accept:application/json
I created middleware for solve this problem - because, in my opinion, api must return json in all cases, if you support only json schema.
app/Http/Middleware/ForceJsonResponse.php
<?php
namespace App\Http\Middleware;
use Closure;
class ForceJsonResponse
{
public function handle($request, Closure $next)
{
$request->headers->set('Accept', 'application/json');
return $next($request);
}
}
app/Http/Kernel.php
...
protected $routeMiddleware = [
//other middlewares
'json' => \App\Http\Middleware\ForceJsonResponse::class,
];
...
protected $middlewareGroups = [
'api' => [
//other middlewares
'json'
],
];
...
Upvotes: 3