【问题标题】:Guzzle POST request: required body when execute requestGuzzle POST 请求:执行请求时需要的正文
【发布时间】:2019-10-09 08:36:56
【问题描述】:

我有一个像这样的 Guzzle POST 请求:

// Return a collection
$cart = $this->getCart('2019-10-08 07:08:39');

//Return first entry of the collection with first()
$template = $this->getTemplate($config->key);

        $isDetail = null;

        foreach ($cart as $item) {
            try {
                $client = $this->getClient();

                $headers = ['Content-Type' => 'application/json'];

                $body = [
                    'user_id'   => $item->mystore_user_id,
                    'title'     => $template->title,
                    'message'   => $template->message,
                    'avatar'    => $template->avatar,
                    'detail_id' => $isDetail,
                    'schedule'  => null
                ];

                print_r($body);

                $response = $client->post('push-noti/unicast', $headers, $body);

                print_r(response()->json(json_decode($response->getBody(), true)));

            } catch (QueryException | \Exception $ex) {
                echo "Error!";
            }
        }

我的 body 变量值在打印时存在于每个循环中。但是当我在 $client->post 中使用它时,我的请求返回错误,其中包含 user_id、title、message 是必需的。我真的不知道为什么会这样?

你能告诉我我的代码有什么问题吗?

谢谢!

【问题讨论】:

  • 尝试将$body替换为[ 'form_params' => $body]
  • 它仍然有这个错误,需要 3 个第一个字段。好奇怪!
  • 你调用你的webservice还是第三方WS?

标签: laravel guzzle


【解决方案1】:

试试

 $response = $client->post('push-noti/unicast', ['body' => $body , 'headers' => $headers]);

如果您正在调用第三方 API,请将 push-noti/unicast 替换为完整的 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2020-07-14
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    相关资源
    最近更新 更多