Skip to main content

Drupal Http API Client and howto pass form-params with options as body to Get Request API Call

Recently i had to make a GET API request with some params (form-params) to the body of the API request call. This is what worked for me:


  public function poi_call(int $id,$timestamp_start = '2020-01-01 00:00:00',$timestamp_end = '2020-12-01 00:00:00') {

    try {

      $options =  [
        'form_params' =--> [ //%Y-%m-%d %H:%M:%S 'timestamp_start' => $timestamp_start, 'timestamp_end' => $timestamp_end, ], ]; $response = $this->client->get('poi/'.$id.'/stats',$options); $data = Json::decode($response->getBody()); return $data; } catch (RequestException $e) { watchdog_exception('people_flow_module', $e); } } 

You can pass the body params with the $options array variable as ['form_params'].

guzzle api rest