Reputation: 139
I just created a new laravel project to be used as API but when I create a post route into the api.php
file it's not working when I test it with Postman.
When I try this code :
Route::post('/announcement', function (Request $request) {
return "OK";
});
I get "OK" in response : Postman result
But when I try this one :
Route::post('/announcement', function (Request $request) {
$request->validate([
"departure" => "required",
"destination" => "required",
"capacity" => "required",
"price" => "required"
]);
$generated = [
"uuid" => (string) Str::uuid()
];
$a = new Announcement(array_merge($request->all(), $generated));
return $a->save();
});
The server redirects me to the home page, I dont really understand what's happening...
Postman result : Postman result
CURL result :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="refresh" content="0;url='http://localhost:8000'" />
<title>Redirecting to http://localhost:8000</title>
</head>
<body>
Redirecting to <a href="http://localhost:8000">http://localhost:8000</a>.
</body>
</html>
I tried to create an other project or to clear the cache but still the same problem. I'm really stuck and I don't know what to do
Upvotes: 0
Views: 555
Reputation: 636
in api its better to use this validation:
$validatedData = Validator::make($request->all(), [
"departure" => "required",
"destination" => "required",
"capacity" => "required",
"price" => "required"
]);
if ($validatedData->fails()) {
return $validatedData->failed();
}
in this case you can send any response you want but when you use $this->validate
it manually redirects you somewhere else when validation is failed
Upvotes: 1