Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stephen waweru's avatar

how to post data to an api using Guzzle/http client from a function in laravel

i have 2 web systems on development,system A and system B.in system A upon creating a product its stored in the products table and at the same time pushed to a same products table in system B.i am using GuzzleHttpClient() connect to api that posts the data to the system b.on die dump am getting the data that i have created in an array but the problem is the data isnt being pushed to the api that posts the data.instead am getting an error Server error: POST http://systemb/api/push_products resulted in a 500 Internal Server Error response:..i have understood where the error is coming from.the url is being read in the http client but the data isnt being pushed.

here is my function that creates the data

        $product_details=new Product;
        $product_details->product_title=$request->product_title;
        $product_details->product_details=$request->product_details;
        $product_details->save();

        // http client request to push data to systemb
        
        $client = new \GuzzleHttp\Client();
        $url = "http://systemb/api/push_products";

        $input = $request->all();
 
 
        //Post to server
        $response = $client->request('POST',$url,[
            $input
        ]);

on die dump the $input i get an array of all the product details have created.

this is my push_data function in the api

     public function push_data(Request $request)
    {
        $product_details=new Product;
        $product_details->product_title=$request->product_title;
        $product_details->product_details=$request->product_details;
       $product_details->save();
    }

i havent understood why the data is not being pushed to the api.kindly assist here fellow devs

0 likes
6 replies
geowrgetudor's avatar

If you use the Guzzle's Client, your 3rd parameter should be an array that can contain headers, body, form_params:

Example:

$response = $client->request('POST',$url,[
    'headers' => [],
    'body' => $input
]);

Try to use Illuminate\Support\Facades\Http facade which is a wrapper for Guzzle Client. Docs here: https://laravel.com/docs/8.x/http-client

stephen waweru's avatar

@geowrgetudor the laravel version of my project is 5.8 and the version my httpclient is 7.4.2..i have imported the facade class ..about the explanation on the 3rd paramater i havent understood how you well

stephen waweru's avatar

@geowrgetudor it shows the 200 status code..

         ^ GuzzleHttp\Psr7\Response {#2613
          -reasonPhrase: "OK"
          -statusCode: 200
         -headers: array:9 [
       "Date" => array:1 [
      0 => "Sat, 12 Feb 2022 09:57:55 GMT"
       ]
    "Server" => array:1 [
      0 => "Apache/2.4.52 (Win64) OpenSSL/1.1.1m PHP/7.4.27"
   ]
   "X-Powered-By" => array:1 [
     0 => "PHP/7.4.27"
   ]
  "Cache-Control" => array:1 [
       0 => "no-cache, private"
   ]
     "X-RateLimit-Limit" => array:1 [
     0 => "60"
   ]
         "X-RateLimit-Remaining" => array:1 [
         0 => "59"
    ]
       "Access-Control-Allow-Origin" => array:1 [
        0 => "*"
     ]
      "Content-Length" => array:1 [
         0 => "2"
      ]
     "Content-Type" => array:1 [
      0 => "text/html; charset=UTF-8"
   ]
 ]
      -headerNames: array:9 [
      "date" => "Date"
        "server" => "Server"
        "x-powered-by" => "X-Powered-By"
        "cache-control" => "Cache-Control"
      "x-ratelimit-limit" => "X-RateLimit-Limit"
       "x-ratelimit-remaining" => "X-RateLimit-Remaining"
        "access-control-allow-origin" => "Access-Control-Allow-Origin"
        "content-length" => "Content-Length"
        "content-type" => "Content-Type"
   ]
    -protocol: "1.1"
     -stream: GuzzleHttp\Psr7\Stream {#2610
       -stream: stream resource @682
        wrapper_type: "PHP"
        stream_type: "TEMP"
        mode: "w+b"
       unread_bytes: 0
         seekable: true
        uri: "php://temp"
        options: []
      }
      -size: null
     -seekable: true
     -readable: true
      -writable: true
      -uri: "php://temp"
     -customMetadata: []
 }

}

Please or to participate in this conversation.