【问题标题】:Making Guzzle HTTP POST Request in Laravel在 Laravel 中制作 Guzzle HTTP POST 请求
【发布时间】:2020-03-28 09:08:12
【问题描述】:

我正在尝试向示例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

我做错了什么??

【问题讨论】:

  • 错字修复,它是dd($response); 而不是dd($response
  • 你能把提交给这个 store 方法的表单贴出来吗?我怀疑在您访问 Guzzle 客户端之前就出现了错误

标签: laravel api guzzle


【解决方案1】:

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

的输出
dd($reponse);

我猜你的表单调用 Post::store 路由有问题,你的表单看起来像是发出 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')
    ]
]);

【讨论】:

  • 当我删除 dd($reponse); 时,错误消失了,但是当我访问我的 enpoint url 时,我看不到新提交的数据。为什么我的数据没有注册?
  • 你做端点?您可以在此处发布处理此 URL cdc-npin.lndo.site/api/nhtd-event/json 的路由和控制器?
【解决方案2】:

这不是表单值;

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

【讨论】:

  • 我不确定这是否是问题所在,因为它应该会导致模板呈现错误,但您在 action 属性中的表单定义中有错字。 route(store') 应该是 route('store')
  • 绝对不是问题的原因!
猜你喜欢
  • 2020-07-14
  • 1970-01-01
  • 2018-03-21
  • 2018-11-02
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 2018-12-06
  • 2018-12-22
相关资源
最近更新 更多