cnuvadga
cnuvadga

Reputation: 109

Making Guzzle HTTP POST Request in Laravel

I am trying to make a post request to the example url endpoint but I keeping running into this weird error "The GET method is not supported for this route. Supported methods: POST." Bellow is my code route and controller code:

Route Snippet: Route::post('/posts', 'PostController@store')->name('store');

Controller Snippet:

public function store(){
        $client = new \GuzzleHttp\Client();
        $url = "https://cdc-npin.lndo.site/api/nhtd-event/json";
        $response = $client->post($url, [
            'form_params' => [
                'key1' =>  'value1',
                'key2' =>  'value2',
                'key3' =>  'value3',
                'key4' =>  'value4',
            ]
        ]);
         dd($response

What am I not doing correctly??

Upvotes: 1

Views: 3106

Answers (2)

Antonio Valle
Antonio Valle

Reputation: 46

"The GET method is not supported for this route. Supported methods: POST." Not is the output of

dd($reponse);

I guess that you have a problem with the Form that calls the route Post::store, your form seems like make a GET Request instead a POST Request, cause the error is from controller not of Guzzle.

I see your form, the form and method is correct...

What if you change the constructor of Guzzle by

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'https://cdc-npin.lndo.site/api/nhtd-event/json', [
    'form_params' => [
        'first_name' => $request->get('first_name'),
        'last_name' => $request->get('last_name'),
        'email' => $request->get('email'),
        'job_title' => $request->get('job_title'),
        'city' => $request->get('city'),
        'country' => $request->get('country')
    ]
]);

Upvotes: 1

cnuvadga
cnuvadga

Reputation: 109

This is not form value;

'form_params' => [
                'key1' =>  'value1',
                'key2' =>  'value2',
                'key3' =>  'value3',
                'key4' =>  'value4',
            ]

with form values, controller looks like

     public function store(Request $request){
        $client = new \GuzzleHttp\Client();
        $url = "https://cdc-npin.lndo.site/api/nhtd-event/json";
        $response = $client->post($url, [
            'form_params' => [
                'first_name' => $request->get('first_name'),
                'last_name' => $request->get('last_name'),
                'email' => $request->get('email'),
                'job_title' => $request->get('job_title'),
                'city' => $request->get('city'),
                'country' => $request->get('country')
            ]
        ]);
         dd($response);
    }

and the form looks like:

<form method="post" action="{{ route('store') }}">
          @csrf
          <div class="form-group">    
              <label for="first_name">First Name:</label>
              <input type="text" class="form-control" name="first_name"/>
          </div>

          <div class="form-group">
              <label for="last_name">Last Name:</label>
              <input type="text" class="form-control" name="last_name"/>
          </div>

          <div class="form-group">
              <label for="email">Email:</label>
              <input type="text" class="form-control" name="email"/>
          </div>
          <div class="form-group">
              <label for="city">City:</label>
              <input type="text" class="form-control" name="city"/>
          </div>
          <div class="form-group">
              <label for="country">Country:</label>
              <input type="text" class="form-control" name="country"/>
          </div>
          <div class="form-group">
              <label for="job_title">Job Title:</label>
              <input type="text" class="form-control" name="job_title"/>
          </div>                         
          <button type="submit" class="btn btn-primary-outline">Add contact</button>
      </form>

Upvotes: 1

Related Questions