在 Laravel 中发送带有 json 对象数据的 post 请求

时间:2021-08-01 13:20:33

标签: php json laravel api

我想在 Laravel 中使用 Http 门面发送请求,但我无法传递 json 对象数据,它必须只是数组。

Http::withHeaders(['Content-Type' => 'application/json'])->withOptions([
        'headers' => $coockie
    ])->post($this->policy_url . $address, json_encode($data));

我的数据变量值:

        $data = [
        "document" => [
            "customerId" => 1,
            "currencyId" => 1,
            "settlementPolicyId" => 8,
            "storeId" => 16,
            "documentPatternId" => 2,
            "items" => [
                [
                    "productId" => 193,
                    "unitId" => 2,
                    "quantity" => 1,
                    "storeId" => 16,
                    "TrackingFactor1" => "1214",
                    "PartTrackingFactorRef1" => 1053,
                    "TrackingFactorHasQuantity1" => true,
                    "TrackingFactorValue" => "‏test",
                    "fee" => 0
                ]
            ]
        ],
        "payments" => [
            [
                "key" => "Cash",
                "amount" => 445810,
                "attr" => []
            ]
        ]
    ];

错误:

TypeError Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given

1 个答案:

答案 0 :(得分:0)

在将其发送到请求之前先生成 <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.0.42.Final</version> </dependency>

json_encode($data)