I'm familiarizing myself with API's Get & Post Request in Laravel. I have a form where an admin can send Points to an external API. Unfortunately, in the documentation the request has to be a GET request and the data which is being submitted has to append to the url ie: myurl.com/my-end-point?platform_id=value&auth=value...
The issue is, whenever i fill the form and submit the url remains the same: ie: myurl.com/my-end-point?platform_id=platform_id&auth=auth...
It does not pass the data in the get request.
Controller
public function sendCpdPoints(Request $request)
{
$response = Http::asForm()->get('myurl.com/my-end-point', [
'platform_id' => 'platform_id',
'auth' => 'auth',
'cpd_id' => 'cpd_id',
'registration_number' => 'registration_number',
'certificate' => 'certificate',
]);
dd($response);
}
View (Blade)
<form action="{{url('admin/rewards/points/manual_update/dashboard/store/participants-points') }}" method="POST">
<div >
{{ csrf_field() }}
<div >
<label>Platform Id</label>
<input type="text" name="platform_id" id='platform_id' value="123" readonly>
</div>
<div >
<label>Authentication Key</label>
<input type="text" name="auth" id='auth' value="123456789" readonly>
</div>
<div >
<label>Cpd Id</label>
<input type="text" name="cpd_id" id='cpd_id' placeholder=".ie 587">
</div>
<div >
<label>Registration Number</label>
<input type="text" name="registration_number" id='registration_number' placeholder=".ie MDC/PA/RNXXXX">
</div>
<div >
<label>Certificate Serial Number</label>
<input type="text" name="certificate" id='certificate' placeholder=".ie 2022-03/001">
</div>
<button type="send" >Send</button>
</div>
</form>
Help is greatly appreciated. As i really want to get better.
CodePudding user response:
You need to pass the value from the form . You can use $request->
from the request facade
You can try this
public function sendCpdPoints(Request $request)
{
$response = Http::asForm()->get('myurl.com/my-end-point', [
'platform_id' => $request->platform_id,
'auth' => $request->auth,
'cpd_id' => $request->cpd_id,
'registration_number' => $request->registration_number,
'certificate' => $request->certificate,
]);
dd($response);
}
CodePudding user response:
Let me see if I understand. Does your backend that handles the request call an external API? If that's the case, the problem is that you're passing strings as an argument. To retrieve the form values try something like: $request->auth or $request->get('auth')
If this is not the case, and you simply want to get the form data in your backend, I advise you to do it with JS using AJAX. You can use Fetch or install an external lib like axios to do this.
NOTE: the type of your submit button is incorrect, change it to type="submit" https://www.w3schools.com/tags/att_button_type.asp