So here's my code on the controller:
public function postAgenda(Request $request)
{
$response = Http::withToken('31|14gfAJHaXv6qU3x4WwxAwE8Txxxxxxxxxxxxxxxx')->post('https://myapi.tld/AgendaCrt', [
'Agenda' => [
'hari' => 'senin',
'tgl' => '2022-02-18',
'waktu' => '16:00:00',
'lokasi' => 'bogor',
'kegiatan' => 'Belajar HTTP Client',
'user_id' => '9'
]
]);
return response()->json([
'Success' => $response
], Response::HTTP_OK);
}
And here's the route on the web.php route:
Route::get('/post', [AgendaController::class, 'postAgenda']);
When i try to run it on browser, the response was success but it doesn't send any data, like the picture below.
CodePudding user response:
Laravel's HTTP client wrapper does not throw exceptions on client or server errors (400
and 500
level responses from servers). You may determine if one of these errors was returned using the successful
, clientError
, or serverError
methods.
Or..
The throw
method returns the response instance if no error occurred, allowing you to chain other operations onto the throw method:
$response = Http::withToken('31|14gfAJHaXv6qU3x4WwxAwE8Txxxxxxxxxxxxxxxx')
->post('https://myapi.tld/AgendaCrt', [
'Agenda' => [
'hari' => 'senin',
'tgl' => '2022-02-18',
'waktu' => '16:00:00',
'lokasi' => 'bogor',
'kegiatan' => 'Belajar HTTP Client',
'user_id' => '9'
]
])
->throw()
->json();
return response()->json([
'Success' => $response
];
CodePudding user response:
Change your method Route::get
to Route::post