Consuming rest api in laravel using guzzle

i am implementing a web based calculator that consumes a rest api and am using guzzle. the webservice only accepts get requests. I would like to get the data from a html form and send it to the webservice to calculate and return me the result based on the arithmetic operation selected. am using laravel. my code is as below

  public function getRemoteData(Request $request)
        $data = $request->all();

        $operation = $data['operation'];
        $num1 = $data['num1'];
        $num2 = $data['num2'];
        $client = new Client();

       $request = $client->get("");
        $response = $request->getBody()->getContents();
        return $response;


my route
Route::get(‘/data’, ‘calculator@getRemoteData’);

my html form

<form action="/data" method="get">
        <div class="form-row">
          <div class="form-group col-md-6">
            <input type="number"  name="num1" class="form-control" id="num1" placeholder="first number">
          <div class="form-group col-md-6">
            <input type="number" name="num2" class="form-control" id="num2" placeholder="Second number">
        <div class="form-row">
          <div class="form-group col-md-6">
            <input type="number" name="result" class="form-control" id="result" placeholder="Result will be here" readonly>
          <div class="form-group col-md-6">
            <select id="inputState" name="operation" class="form-control">
              <option selected>Choose an operation to perform</option>
        <button type="submit" class="btn btn-primary">Calculate</button>
        <button type="submit" class="btn btn-info">Clear</button>

So you posted your concept to us. What now?

No. I need help on how to pass on form data to the api. it only accepts get request

Instead of using $client->get(), you should use the $client-post() method and specify the form fields as an array in the form_params request option. So something like this should work:

$request = $client->post('', [
    'form_params' => [
        'num1' => $num1,
        'num2' => $num2,

You receive the data via $_POST or any middleware you use, maybe $request and can then give this data to Guzzle, as the documentation states:

Please ignore myself previous post. I misread your question.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.