Drupal http get Request for external URL? I have to send a request to external Api that returns me the redirection page but it is printed on my side as var dump, the dump is the page it i supposed to be redirected to, any help ?

Code :

$url = 'xyz';

try {
  $response = \Drupal::httpClient()-&gt;get($url, ['headers' =&gt; ['Accept' =&gt; 'text/html']]);
  $data = (string) $response-&gt;getBody();
  if (empty($data)) {
    return FALSE;
  }else{
    return $data;
  }
} catch (RequestException $e) {
  return FALSE;
}


Returned $data looks something like this :

    &lt;!doctype html&gt;
&lt;html lang="en" dir="ltr" class="h-100"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=Edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
    &lt;meta name="description" content=""&gt;
    &lt;meta name="author" content="asdsad"&gt;
    &lt;!-- CSRF Token --&gt;
    &lt;meta name="csrf-token" content="aaaaa1231231"&gt;
    &lt;title&gt;TEST&lt;/title&gt;
    &lt;link rel="canonical" href="tamal.com"&gt;
    &lt;!-- Bootstrap core CSS --&gt;
    &lt;link href="http:xyz.css" rel="stylesheet"&gt;
this actually because of the second argument ['headers' => ['Accept' => 'text/html'] it tells the server to serve a plain text instead of html so your browser will end up print the text instead of parsing it as HTML,so just remove the second argument ['headers' => ['Accept' => 'text/html'] 

so you code will become like this one :

Code :

$url = 'xyz';

try {
  $response = \Drupal::httpClient()-&gt;get($url);
  $data = (string) $response-&gt;getBody();
  if (empty($data)) {
    return FALSE;
  }else{
    return $data;
  }
} catch (RequestException $e) {
  return FALSE;
}