In Laravel, send a post request with json object data

In Laravel, send a post request with json object data

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing the PendingRequest::post() TypeError When Sending JSON Payloads

When working with Laravel's HTTP client facade, a common stumbling block arises when attempting to send nested JSON objects. Developers often encounter a TypeError stating that the second argument passed to PendingRequest::post() must be an array, but a string was given. This specific error occurs because the underlying implementation of the Http facade expects the payload to be a native PHP array, not a serialized string, even if you explicitly set the Content-Type header to application/json.

In the scenario provided, the developer attempts to pass json_encode($data) directly as the second argument to the post() method. While this works in many raw HTTP libraries like Guzzle or cURL, Laravel's Illuminate\Http\Client abstracts this behavior. The facade handles serialization internally. When you manually encode the data into a string and pass it along with the Content-Type: application/json header, you are overriding the framework's internal logic, which expects to manage the payload formatting itself.

To resolve the error, you must pass the $data variable as a native PHP array without calling json_encode(). Laravel will automatically serialize this array into JSON and set the appropriate headers if you haven't already specified them. Here is how the corrected code looks:

use Illuminate\Support\Facades\Http;

$payload = [
    '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', // Note: trimmed whitespace for safety
                'fee' => 0
            ]
        ],
        'payments' => [
            [
                'key' => 'Cash',
                'amount' => 445810,
                'attr' => []
            ]
        ]
    ]
];

$response = Http::withHeaders([
    'Content-Type' => 'application/json',
    'Cookie' => $cookies // Assuming $cookies is your variable
])->post($this->policy_url . $address, $payload);

By passing $payload directly, you allow the PendingRequest instance to handle the encoding. This approach ensures that special characters are escaped correctly and that the resulting JSON structure adheres strictly to RFC 8259 standards, which manual encoding might sometimes compromise depending on the library version or specific character sets used.

There is a distinct trade-off here between control and convenience. Manually using json_encode() gives you granular control over the encoding process, such as selecting indentation levels or specific JSON libraries, but it requires you to manage headers manually. Relying on Laravel's facade simplifies this by decoupling the data structure from the transport layer logic. If you need to send a pre-formatted string (for example, to preserve exact whitespace for an API that is picky about formatting), you would need to switch to using GuzzleHttp\Client directly rather than the Http facade, bypassing the type hints enforced by Laravel's abstraction.

Another pitfall to watch out for involves mixed content types. If your external API accepts both JSON and form data (multipart/form-data) depending on the endpoint, passing a raw string forces the request into a text/plain or specific charset format unless headers are meticulously managed. Passing an array ensures the Content-Type is strictly application/json, preventing ambiguity at the server side.

If you are integrating with third-party services that require complex nested structures like the one in your example, ensuring the data is passed as an associative array is crucial for maintaining compatibility with Laravel's internal middleware and logging systems. For further guidance on handling external API integrations within the Laravel ecosystem, resources like https://laravelcompany.com often provide community-driven patterns that align with these best practices.

Our network