【问题标题】:How do I pass apikey and other keys to header in guzzle 6.3?如何在 guzzle 6.3 中将 apikey 和其他键传递给标题?
【发布时间】:2020-07-14 16:18:03
【问题描述】:

我有一个简单的注册表单,用户可以在我的应用中注册,现在我想将提交的数据发送到另一个服务。

首先,我使用邮递员面板中的原始选项测试我的请求,如下所示。

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",
      }
    }
}

UPDATE 我得到以下成功结果

{
    "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']),
        ]);

        $client = new client();
        $current_timestamp = Carbon::now()->timestamp;
        try {
            $request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
                \GuzzleHttp\RequestOptions::HEADERS      => array(
                    'debug'        => true,
                    'Accept'       => 'application/json',
                    'Content-Type' => 'application/json',
                    'clientId'     => 's255hncrw06k7ny45umc',
                    'apiKey'       => 'sj2q8rt5qp4fbp9qf2b8p49fz',
                    'sha'          => 'ba0br45543dcaf5ca82b09e33264fedb509cfb4806c',
                    'requestTime'  =>  $current_timestamp,
                    'owner'        => 'adwamtrw@fere.com',
                    'http_error'   => true
                ),

                \GuzzleHttp\RequestOptions::JSON => [
                    'form_params' => [
                        'name'  => $data['name'],
                        'email' => $data['email'],
                    ],
                ],
            ]);  
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $responseBodyAsString = $response->getBody()->getContents();
        }
        $status = $request->getStatusCode();
        $response = $request->getBody();
        $r = json_decode($response);
        dd($r);
        dd($status, $r );
    return $user;
}

当我运行我的应用程序并发送表单数据时,我使用与邮递员相同的数据得到这个

{#306 ▼
  +"success": false
  +"message": array:1 [▼
    0 => "Not authenticated"
  ]
  +"contactId": null
  +"externalId": null
}

好像我的API key和其他header数据没有按要求传递给header,

谁能告诉我我在这里做错了什么?

【问题讨论】:

  • 为什么headers\GuzzleHttp\RequestOptions::JSON里面?
  • @FelippeDuarte 你能提供正确的方法吗?
  • 您作为json数据发布的第一个sn-p属于传递给api的标头还是POST请求的主体?因为如果它是有效负载而不是标头,那么您应该修改下面的代码以将身份验证值作为正文的一部分发送,而不是像您现在在 Guzzle 中所做的那样发送标头。
  • @DraQ 上面的 sn-ps 来自 API 文档,我只是更改并添加了我的 API 密钥、sha 等,并将其发布在邮递员中,运行 post 给我成功,但在 guzzle 中,我有错误,你能以代码的形式显示你在说什么吗?
  • header 和 body 是有区别的。你如何在邮递员中填写这些信息?作为标题还是作为正文?如果是标题,则需要将headers 键移到\GuzzleHttp\RequestOptions::JSON 之外。

标签: php laravel laravel-5 guzzle guzzle6


【解决方案1】:

也许是这样的。请注意,根据 API,某些值应作为标头传递(Accept 和 Content-Type - 通常用作标头,顺便说一句),而其他值应作为正文的一部分。像 clientId 和 apiKey 这样的身份验证值就是这种情况。

我手头没有安装 guzzle 6,但您可以尝试修改代码以将数据包含在请求的标头部分而不是正文中:

$request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
        \GuzzleHttp\RequestOptions::HEADERS      => array(
            'debug'        => true,
            'Accept'       => 'application/json',
            'Content-Type' => 'application/json',
        ),

        \GuzzleHttp\RequestOptions::JSON => [
            'form_params' => [
                'name'  => $data['name'],
                'email' => $data['email'],
                'clientId'     => 's255hncrw06k7ny45umc',
                'apiKey'       => 'sj2q8rt5qp4fbp9qf2b8p49fz',
                'sha'          => 'ba0br45543dcaf5ca82b09e33264fedb509cfb4806c',
                'requestTime'  =>  $current_timestamp,
                'owner'        => 'adwamtrw@fere.com',
                'http_error'   => true
            ],
        ],
    ]);

我不确定RequestOptions::JSON 下的“form_params”,但您可以将值直接放在RequestOptions::JSON 下。

【讨论】:

  • 我也试过了,但不幸的是,我得到了同样的回应
  • 您是否也尝试过不使用“form_params”级别? \GuzzleHttp\RequestOptions::JSON => ['name'=>"name", ..., ]
  • 感谢您解决了问题,在删除表单参数后,我需要 API 参数 contact 并传递这些表单数据名称,现在里面的电子邮件可以正常工作了
【解决方案2】:

仅供参考,不确定您使用的是什么 Laravel,但现在有 The Laravel HTTP client 让这变得更加容易。

$response = Http::withHeaders([
                'Accept' => 'application/json, application/json',
                'Content-Type' => 'application/json',
                'clientId' => 'dd2ncrw06k7ny45umce',
                'apiKey' => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
                'sha' => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
                "requestTime" => $current_timestamp,
                "owner" => "testemail@wp.com",
])->post('app3.salesmanago.pl/api/contact/upsert', [
    'name' => $data['name'],
    'email' => $data['email'],
]);

if($response->successful()){
 dd($response->json())
}else{
  // handle yo errors
}

【讨论】:

  • 我收到以下错误Class 'Illuminate\Support\Facades\Http' not found
  • 你使用的是哪个版本的 Laravel?
  • 6.3 我已经安装了
  • 正确的答案是您不应该在标头中发送您的 api 凭据,而是作为@DraQ 所说的数据变量。
  • 我试过你的脚本,但出现上述错误,我已经按照@draq 进行了更改
猜你喜欢
  • 2022-12-09
  • 2018-09-21
  • 1970-01-01
  • 2019-03-22
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多