从Lumen API中的Guzzle发布请求中获取form_params数据

时间:2018-11-11 12:59:02

标签: laravel lumen guzzle

在我的laravel 5.7应用程序中,我有一个控制器,该控制器具有以下方法,该控制器使用GuzzleHttp \ Client向我的内腔api发送发布请求。

public function insert(Request $request)
    { 
        $domain = Config::get('url.gcr_api'); // gcr_api is defined in app/config/url.php
        $url = $domain . "/api/v1/campaign/insert"; 

        $campaign_name =  $request->input('campaign_name');
        $campaign_provider =  $request->input('campaign_provider');

        $client = new Client;
        $response = $client->request('POST', $url , [
                                'headers' => [
                                    'X-XSRF-TOKEN' => csrf_token(),
                                ],
                                'form_params' => [
                                    'name' => $campaign_name, 
                                    'bank_id' => $campaign_provider,
                                ]
                            ]
                    );

        $data = json_decode($response->getBody(), true);
        return $data;

    }
}

这是上述方法调用的lumen api的代码。

public function insert(Request $request) {
        try {
            $campaign_name =  $request->input('campaign_name');
            $campaign_provider =  $request->input('campaign_provider');

            $data = array('name' => $campaign_name, 'bank_id' => $campaign_provider);
            DB::table('campaign')->insert($data); 

            $response = array(
                'status' => 'success',
                'message' => 'Campaign created successfully',
            );        
            return response()->json($response);
        }
        catch(Exception $e) {
            // When query fails. 
            $response = array(
                'status' => 'failed',
                'message' => 'Failed to create campaign',
            );

            return response()->json($response);
        }
    }

每次调用laravel应用控制器的insert方法时,我都会收到此错误:

  

GuzzleHttp \ Exception \ ServerException(500)服务器错误:POST http://localhost:8000/api/v1/campaign/insert导致500 Internal Server Error响应:

我认为该错误与我的内腔api无法获取form_params数据有关,因此我在我的内腔api代码中注释掉了这段代码,错误消失了:

/*$campaign_name =  $request->input('campaign_name');
  $campaign_provider =  $request->input('campaign_provider');

  $data = array('name' => $campaign_name, 'bank_id' => $campaign_provider);
  DB::table('campaign')->insert($data); */

但是我需要在流明api中获取form_params数据,该怎么做?

1 个答案:

答案 0 :(得分:0)

您的假设部分正确,可以访问form_params,但是您尝试获取的form_params与实际参数有所不同:

为了传递参数,请使用以下数组:

'form_params' => [
    'name' => $campaign_name, 
    'bank_id' => $campaign_provider,
]

此数组将使用键“名称”将值设置为$campaign_name,将$campaign_provider设置为bank_id。到目前为止,这段代码没有错。

不过,您可以在API中执行以下操作:

$campaign_name =  $request->input('campaign_name');
$campaign_provider =  $request->input('campaign_provider');

但是没有键campaign_namecampaign_provider,因为它们已更改为namebank_id。因此,如果将其更改为以下内容,则应该可以使用:

$campaign_name =  $request->input('name');
$campaign_provider =  $request->input('bank_id');