我正在向我创建的一个PUT
端点发送一个API请求。使用jwt,我能够成功注册并获得令牌。
使用Postman,我的请求可以完美地工作。
我在应用程序中使用Guzzle发送PUT
请求。它看起来是这样的:
$client = new \Guzzle\Http\Client('http://foo.mysite.dev/api/');
$uri = 'user/123';
$post_data = array(
'token' => eyJ0eXAiOiJKV1QiLCJhbGc..., // whole token
'name' => 'Name',
'email' => name@email.com,
'suspended' => 1,
);
$data = json_encode($post_data);
$request = $client->put($uri, array(
'content-type' => 'application/json'
));
$request->setBody($data);
$response = $request->send();
$json = $response->json();
} catch (\Exception $e) {
error_log('Error: Could not update user:');
error_log($e->getResponse()->getBody());
}
当我记录$data
变量以查看其外观时,返回的内容如下所示。
error_log(print_r($data, true));
{"token":"eyJ0eXAiOiJKV1QiL...","name":"Name","email":"name@email.com","suspended":1}
Error: Could not suspend user:
{"error":"token_not_provided"}
似乎所有数据都被正确填充,我不确定为什么系统找不到令牌。使用相同的参数通过Postman (作为PUT
)运行“相同”的查询效果很好。
任何建议都是非常感谢的!
发布于 2016-08-21 15:47:08
应在authorization标头中设置令牌,而不是将其设置为post数据参数
$request->addHeader('Authorization', 'Basic eyJ0eXAiOiJKV1QiL...');
https://stackoverflow.com/questions/39047645
复制相似问题