The available API methods are coded in ways that aren’t suitable for raw data payloads. postJson() and even post() type hint the data argument as an array, and both forward data as the parameter argument to call().
This snippet shows how you can leverage the underlying call() method available in Laravel’s PHPUnit TestCase class. Pest users, note that tests implicitly extend TestCase.
/**
* This is run within a test.
*/
$payload = file_get_contents('path/to/image.png');
$this->call(
'POST',
'/api/endpoint/route',
[], // Parameters
[], // Cookies
[], // Files
[ // Server
'HTTP_CONTENT_LENGTH' => mb_strlen($payload, '8bit'),
'CONTENT_TYPE' => 'image/*',
'HTTP_ACCEPT' => 'application/json'
],
$payload
); // Assertions can be chained as usual