Global variable assigned inside while loop not available outside the loop
<p>I'm calling an api using ESP8266 WiFi Chip. Im declaring a global variable to assign the token sent by the server to it. The variable assignment is made inside a while loop. In this loop i'm reading the contents of the reply "the token" and saving it in the global variable.
But when i print out the result outside the while loop to variable is empty. But inside the loop it is not.</p>

<pre><code>String deviceToken;

WiFiClient client;

  if (!client.connect(hostIp, 80)){
    Serial.println("Connection to server failed.");
    Serial.print(client.connect(hostIp, 80));
    return;
  }

    String authURL = "/api/";
      authURL += "startauth/";
      authURL += serialValue;
      Serial.print("Requesting URL: ");
      Serial.println(authURL);

      // This will send auth request to the server
      client.print(String("GET ") + authURL + " HTTP/1.1\r\n" +
                   "Host: " + host + "\r\n" + 
                   "Connection: close\r\n\r\n");
     unsigned long timeout = millis();
     while (client.available() == 0) {
        if (millis() - timeout &gt; 5000) {
          Serial.println("&gt;&gt;&gt; Client Timeout !");
          client.stop();
          return;
        }
     }
    // Read Auth reply from server and print them to Serial
      while(client.available()){
        String token = client.readStringUntil('\r');
        deviceToken = token;
        writeDeviceToken(token.c_str());  //Function to write data in eeprom
        Serial.print(deviceToken); //NOT EMPTY
      }

      Serial.print("Device Token:" + deviceToken);  //EMPTY VARIABLE
</code></pre>

<p><a href="https://i.stack.imgur.com/FSUlD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FSUlD.png" alt="enter image description here"></a></p>