HTTP/2 使用 symfony HTTP 客户端发出 post 请求时 URL 返回 400

HTTP/2 400 returned for URL when using symfoy HTTP Client to make post request

我想将使用 post man 测试的 http post 请求转换为 symfony 操作:

我想将有效负载转换为 symfony 中的 json 数组以将数据发送到 url :

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Routing\Annotation\Route;

    class PushNotificationController extends AbstractController
    {
        /**
         * @Route("/api/push_notification", name="push_notification")
         */
        public function index()
        {
            $httpClient = HttpClient::create();
            $response = $httpClient->request('POST', 'https://fcm.googleapis.com/fcm/send', [
                'headers' => [
                    // 'Accept' => 'application/json',
                    // "Content-Type" => "application/json; charset=UTF-8",
                    'Authorization' => 'Bearer token'
                ],
                'json' => [
                    'notification' => [
                        'title' => 'Portugal vs. Denmark',
                        'message' => 'My Notification Message',
                        'body' => '5 to 1',
                    ],
                    'token' => 'token'
                ],
            ]);
    
            $statusCode = $response->getStatusCode();
            // $statusCode = 200
            $contentType = $response->getHeaders()['content-type'][0];
            // $contentType = 'application/json'
            $content = $response->getContent();
            // $content = '{"id":521583, "name":"symfony-docs", ...}'
            $content = $response->toArray();
            // $content = ['id' => 521583, 'name' => 'symfony-docs', ...]
    
            return $content;
        }
    }

我遇到了这个错误:

我认为这是关于负载的错误。有什么建议吗?

Error 400 无效 Json 输入:

Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields (for instance, passing a string where a number was expected). The exact failure reason is described in the response and the problem should be addressed before the request can be retried.

所以我想您需要查看您发送的 json。 另外,您可以使用 Knp 或 git.

中的一些 FCM Bundle