How do I format Guzzle POST request

49 Views Asked by At

Guzzle beginner here. I have an API request that I've got working in Postman and the raw CURL code works but when I try the GuzzleHTTP approach, it fails with 406 Not Acceptable response: Could not match accept header

Code is:

    $client = new \GuzzleHttp\Client();
    $headers = [
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer cxxxxxxx'
];

$body = [
  "block"=> "0",
  "email"=> $jemail,
   etc, etc
];

$request = new  \GuzzleHttp\Psr7\Request('POST', 'https://xxxx.uk/api/index.php/v1/users', $headers, json_encode($body));
$res = $client->sendAsync($request)->wait();

I'm probably missing something obvious and would welcome any suggestions.

The equivalent cURL code (taken directly from Postman) which works is:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://xxxx.uk/api/index.php/v1/users',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "block": "0",
    "email": $jemail,
    etc, etc
}',

  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Authorization: Bearer c2xxxxxxx'
  ),
));

$response = curl_exec($curl);

The API definition (It's Joomla 4) is

Create User

curl -X POST -H "Content-Type: application/json" /api/index.php/v1/users -d

{
    "block": "0",
    "email": "[email protected]",
    etc, etc
}
0

There are 0 best solutions below