在Laravel中进行Guzzle HTTP POST请求

时间:2019-12-03 16:15:25

标签: laravel api guzzlehttp

我正在尝试向示例url endpoint发出发布请求,但我一直遇到这个奇怪的错误"The GET method is not supported for this route. Supported methods: POST.",下面是我的代码路由和控制器代码:

路线摘录: Route::post('/posts', 'PostController@store')->name('store');

控制器代码段:

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

我在做什么不正确?

2 个答案:

答案 0 :(得分:1)

这不是表格值;

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

具有表单值,控制器看起来像

     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);
    }

表单如下:

<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>

答案 1 :(得分:1)

“此路由不支持GET方法。受支持的方法:POST。”不是

的输出
dd($reponse);

我猜您在调用路由Post :: store的Form上遇到问题,您的表单似乎是发出GET请求而不是POST请求,导致错误是来自控制器而不是Guzzle。

我看到您的表格,该表格和方法正确...

如果您通过以下方式更改Guzzle的构造函数怎么办

$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')
    ]
]);