Client Error 404 not found when sending a post request with guzzle

7.1k views Asked by At

I want to send a post request to an external api using guzzle , but i get this error :
Client error: POST https://api.platform.ly/ resulted in a 404 Not Found response:

{"status":"error","message":"Missing Parameters"}
$client = new \GuzzleHttp\Client();
         $url = "https://api.platform.ly/";
         $body['api_key'] = ENV('PLATFORMLY_KEY');
         $body['action'] = 'add_contact';
         $body['value'] = [
             'project_id' => '1589',
             'email' => $user->email,
             'name' => $user->name
         ];
         $request = $client->post($url, ['form_params'=>$body]);
         dd($request);
         $response = $request->send();
         dd($response);

1

There are 1 answers

0
Mike Shoebox DnbRadio On BEST ANSWER

This should fix your problem. A JSON string is needed on the value field per platformly docs, so utilize json_encode like so.

$body['value'] = json_encode([
  'project_id' => '1589',
  'email' => $user->email,
  'name' => $user->name
]);

I had the same issue and it took a min to figure it out because it wasn't clear.

Here is your code snippet with json_encode implemented.

$client = new \GuzzleHttp\Client();
         $url = "https://api.platform.ly/";
         $body['api_key'] = ENV('PLATFORMLY_KEY');
         $body['action'] = 'add_contact';
         $body['value'] = json_encode([
             'project_id' => '1589',
             'email' => $user->email,
             'name' => $user->name
         ]);
         $request = $client->post($url, ['form_params'=>$body]);
         dd($request);
         $response = $request->send();
         dd($response);