React Native
React Native
Configure HTTP Basic Authentication for REST
See more REST Examples
Demonstrates Rest.SetAuthBasic, which configures HTTP Basic authentication from a username and password. Chilkat automatically adds the Authorization header to each request sent through the object.
Background. HTTP Basic authentication transmits credentials with every request, so it should be used over HTTPS. Credentials should be obtained from a secure source rather than hard-coded in production code.
Chilkat React Native Downloads
import { Rest } 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;
}
// 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 username = 'myUsername';
const password = 'myPassword';
// Configure HTTP Basic authentication. Chilkat automatically adds the Authorization header to
// each request sent through this object.
try {
rest.setAuthBasic(username, password);
} 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);
}