Laravel调用Smartrmail新增订阅者接口报subscribers参数缺失错误问询
问题原因
- 请求头声明了
Content-Type: application/json,但Guzzle请求中使用了form_params参数,form_params仅用于发送x-www-form-urlencoded格式的表单数据,和声明的请求格式不匹配,发送JSON格式数据需要使用Guzzle的json参数。 - 已经包含
subscribers键的$data被额外套了一层数组,最终发送的参数结构变成了[0 => ['subscribers' => [...]]],服务端无法读取到顶层的subscribers参数,因此报参数缺失错误。
修复后代码
Route::get('smartrmail',function(){ $headers = [ 'Accept' => 'application/json', 'Authorization' => 'token f91715d5-3aac-4db3-a133-4b3a9493a9a4', ]; $client = new GuzzleHttp\Client([ 'headers' => $headers ]); $data = [ "subscribers"=>[ [ "email"=> "vanhalen@example.com", "first_name"=> "van", "last_name"=> "halen", "subscribed"=> true, ] ] ]; $res = $client->request('POST', 'https://go.smartrmail.com/api/v1/lists/1sptso/list_subscribers', [ 'json' => $data ]); return $res; });
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

