How to make post request to an API and retrieve the response

Viewed 26

I'm trying to make a post request in my Command file to an API, which should accept a single key-value pair as a request body and return some statistics in the response body. I'm a beginner in laravel and back-end in general so I'm struggling a little bit with it. When I try to test it on Postman, It works and the body gives me this :

{
    "id": 29,
    "country": "Georgia",
    "code": "GE",
    "confirmed": 2808,
    "recovered": 4123,
    "critical": 4021,
    "deaths": 1730,
    "created_at": "2022-02-13T18:17:01.000000Z",
    "updated_at": "2022-09-08T00:00:03.000000Z"
}

But, when I try to do the exact same thing from my controller, I get the status 200 but I'm not able to see the same response. Here's my command file :

public function handle()
    {
        $client = new Client();
        $res = $client->request('POST', 'https://something', [
            'form-params' => [
                'code' => 'GE',
            ],
        ]);
        dd($res);
    }

When I run this command in the terminal, I get this response :

GuzzleHttp\Psr7\Response^ {#606
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:7 [
    "Server" => array:1 [
      0 => "nginx/1.18.0 (Ubuntu)"
    ]
    "Content-Type" => array:1 [
      0 => "text/html; charset=UTF-8"
    ]
    "Transfer-Encoding" => array:1 [
      0 => "chunked"
    ]
    "Connection" => array:1 [
      0 => "keep-alive"
    ]
    "Cache-Control" => array:1 [
      0 => "no-cache, private"
    ]
    "Date" => array:1 [
      0 => "Thu, 08 Sep 2022 08:25:51 GMT"
    ]
    "Set-Cookie" => array:2 [
      0 => "XSRF-TOKEN=eyJpdiI6IlpJRkwvR2xzY3QveWY0UlArMUpEcGc9PSIsInZhbHVlIjoiTS9EN09GMzZWaFR5ZWJGUVlGUGx4NHljR2lpeWdtU2kxdmxrSjM0R1hiV05rUEl0TDc1c0h0dTVJVGpPeFJ0QmxRVTJzU0g5VEQxVDlXRkRObHRJQTVxeTZpSWI1Ky9MQjB2bjhsWTE1YmVYSlpQYW93MWdRb09qQ21zbGRLR0siLCJtYWMiOiJhZGNlYzcwNGUyMzdmODAyYzExYzEwMWM1ZDBhODQyY2EzOGM1YzY5YjY3MTAyZTQ2MzY2NTg1NWJkNDM3NGZkIiwidGFnIjoiIn0%3D; expires=Thu, 08-Sep-2022 10:25:51 GMT; Max-Age=7200; path=/; samesite=lax"
      1 => "covid_data_session=eyJpdiI6IlliOEY1ZWVjNHBOTGZTaHFMV1lzSlE9PSIsInZhbHVlIjoibkxEY0lNVy9ZdmM1dlFlTzRGb2dYZUtmQnBOQm1pN2g5WTgxR1IzM3lvWXZsZGJJbkRURHJRelBRYVJoTWt2MEhoR2RoZDI3YlVQM3BJYTNMNWd5YUNKQStYVEozTHpLTFZHb0VHd2dldytSa3Q4OHlnSkVIc05pQjJDSmZmU00iLCJtYWMiOiIxZWM3MjhlNTkwOTdiOTQ3NDE3OGZiNTRhZWVkNjdiODY2YzBmMDAyZjdjYWI4MGZlY2UzNmQzNDljMmJlODZkIiwidGFnIjoiIn0%3D; expires=Thu, 08-Sep-2022 10:25:51 GMT; Max-Age=7200; path=/; httponly; samesite=lax"
    ]
  ]
  -headerNames: array:7 [
    "server" => "Server"
    "content-type" => "Content-Type"
    "transfer-encoding" => "Transfer-Encoding"
    "connection" => "Connection"
    "cache-control" => "Cache-Control"
    "date" => "Date"
    "set-cookie" => "Set-Cookie"
  ]
  -protocol: "1.1"
  -stream: GuzzleHttp\Psr7\Stream^ {#600
    -stream: stream resource {@622
      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: []
  }

If I try to dump the body with dd($res->getBody()), I get this response in terminal :

-stream: stream resource {@622
    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: []

What am I doing wrong? Any help would be appreciated, thank you!

0 Answers
Related