我有一个简单的注册表格,用户可以在我的应用程序中注册,现在我想将提交的数据发送到另一个服务。
首先,我在邮递员面板中使用原始选项,如下测试邮递员的请求。
Api网址:app3.salesmanago.pl/api/contact/upsert
JSON DATA:
{
"clientId":"w2ncrw06k7ny45umsssc",
"apiKey":"ssssj2q8qp4fbp9qf2b8p49fz",
"requestTime":1327056031488,
"sha":"ba0ddddddb543dcaf5ca82b09e33264fedb509cfb4806c",
"async" : true,
"owner" : "adam@rce.com",
"contact" : {
"email" : "test-1@konri.com",
"name" : "Test",
"address":{
"streetAddress":"Brzyczynska 123",
}
}
}
我得到以下成功结果
{
"success": true,
"message": [],
"contactId": "b52910be-9d22-4830-82d5-c9dc788888ba",
"externalId": null
}
现在在laravel中使用guuzle htpp请求
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$current_timestamp = Carbon::now()->timestamp;
$client = new Client();
$request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
\GuzzleHttp\RequestOptions::JSON => [
'headers' => [
'Accept' => 'application/json, application/json',
'Content-Type' => 'application/json',
'clientId' => 'dd2ncrw06k7ny45umce',
'apiKey' => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
'sha' => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
"requestTime" => $current_timestamp,
"owner" => "testemail@wp.com",
],
'form_params' => [
'name' => $data['name'],
'email' => $data['email'],
]
]
]);
$response = $request->getBody();
$r = json_decode($response);
dd($r);
return $user;
}
当我运行我的应用程序并发送表单数据时,我使用与邮递员中获得的数据相同的数据来获取此信息
{#306 ▼
+"success": false
+"message": array:1 [▼
0 => "Not authenticated"
]
+"contactId": null
+"externalId": null
}
有人可以告诉我为什么在Postman中一切正常,但是在laravel中却失败了吗?
我的代码有什么问题?
答案 0 :(得分:1)
因为您在json选项中编写了标头。这是正确的
$client = new Client();
$request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
\GuzzleHttp\RequestOptions::JSON => [
'form_params' => [
'name' => $data['name'],
'email' => $data['email'],
],
],
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json, application/json',
'Content-Type' => 'application/json',
'clientId' => 'dd2ncrw06k7ny45umce',
'apiKey' => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
'sha' => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
"requestTime" => $current_timestamp,
"owner" => "testemail@wp.com",
],
]);
答案 1 :(得分:0)
您可以像
一样使用它$client->request('POST', 'app3.salesmanago.pl/api/contact/upsert', [
'form_params' => [
'name' => $data['name'],
'email' => $data['email'],
],
'headers' => [
'Accept' => 'application/json, application/json',
'Content-Type' => 'application/json',
'clientId' => 'dd2ncrw06k7ny45umce',
'apiKey' => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
'sha' => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
'requestTime' => $current_timestamp,
'owner' => 'testemail@wp.com',
]
]);