Sample code for 30+ languages & platforms
React Native

Disconnect from a REST Server

See more REST Examples

Demonstrates Rest.Disconnect, which closes the current HTTP connection. The argument is the maximum number of milliseconds to wait for the close to complete.

Background. Connections are normally kept alive for reuse. Disconnect is used when an application wants to explicitly release the connection rather than relying on keep-alive or object destruction.

Chilkat React Native Downloads

React Native
import { Rest } from '@chilkat/react-native'

async function chilkatExample() {
  const rest = new Rest();

  // Connect to the REST server.  The 3rd argument enables TLS (use true for HTTPS on port 443),
  // and the 4th enables automatic reconnection if the connection is dropped between requests.
  const bTls = true;
  const bAutoReconnect = true;
  try {
    await rest.connectAsync('example.com', 443, bTls, bAutoReconnect);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  let responseText: string;
  try {
    responseText = await rest.fullRequestNoBodyAsync('GET', '/api/status');
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  console.log(responseText);

  // Close the connection.  The argument is the maximum number of milliseconds to wait for the
  // close to complete.
  try {
    await rest.disconnectAsync(200);
  } catch {
    console.log('The disconnect did not complete within the wait time.');
  }

  console.log('Done.');
}