Sample code for 30+ languages & platforms
React Native

Configure HTTP Basic Authentication with SecureString

See more REST Examples

Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.

Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.

Chilkat React Native Downloads

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

async function chilkatExample() {
  const rest = new Rest();
  const bTls = true;
  const bAutoReconnect = true;
  try {
    await rest.connectAsync('example.com', 443, bTls, bAutoReconnect);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  // SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
  // the credentials encrypted in memory.
  // Normally you would not hard-code secrets in source.  You should instead obtain them from an
  // interactive prompt, environment variable, or a secrets vault.
  const ssUsername = new SecureString();
  ssUsername.append('myUsername');
  const ssPassword = new SecureString();
  ssPassword.append('myPassword');

  try {
    rest.setAuthBasicSecure(ssUsername, ssPassword);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

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

  console.log(responseText);
}