0

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()->get($url, ['headers' => ['Accept' => 'text/html']]);
  $data = (string) $response->getBody();
  if (empty($data)) {
    return FALSE;
  }else{
    return $data;
  }
} catch (RequestException $e) {
  return FALSE;
}

Returned $data looks something like this :

    <!doctype html>
<html lang="en" dir="ltr" class="h-100">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="asdsad">
    <!-- CSRF Token -->
    <meta name="csrf-token" content="aaaaa1231231">
    <title>TEST</title>
    <link rel="canonical" href="tamal.com">
    <!-- Bootstrap core CSS -->
    <link href="http:xyz.css" rel="stylesheet">
0

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()->get($url);
  $data = (string) $response->getBody();
  if (empty($data)) {
    return FALSE;
  }else{
    return $data;
  }
} catch (RequestException $e) {
  return FALSE;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.