如何使用Guzzle在我的Laravel API上发布和接收数据

时间:2019-03-06 15:12:57

标签: laravel api request guzzle

我这里有一个很尴尬的问题:

我试图这样调用自己的Laravel API:

$api_url    `enter code here`=  env("API_URL")."login";
$data=  json_encode($data);
$api_url    =   env("API_URL")."login";
$client     =   new \GuzzleHttp\Client();
$headers    =   array('Accept: application/json','Content-Type: application/json', 'X-XSRF-TOKEN' => csrf_token());
$response   =   $client->request('POST', $api_url , ['headers'=>$headers,'json' => $data]);
print_r($response);

然后我的API端点具有如下代码:

public function login(Request $request)
{
//This line gives me NULL
dump($request->all());

//the lines below print this "{"email":"dev@gumption.pk","password":"password01"}"
//but I cannot get this POSTED data in variables even with $request->input('email')
$content = $request->getContent();
print_r($content);

}

我无法从此POSTED数据中检索变量。我已经尝试过$ request-> input('email'); $ request-> json('email');

什么都没用。它只是说NULL。

我不确定这是怎么回事。

0 个答案:

没有答案