React Native Requires Chilkat v11.0.0+
React Native
REST through SSH Tunnel
See more REST Examples
Demonstrates how to connect through an SSH Tunnel (via port-forwarding) to make REST API calls.Chilkat React Native Downloads
import { AuthAws, Rest, Socket, Xml } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const tunnel = new Socket();
const sshHostname = 'sftp.example.com';
const sshPort = 22;
// Connect to an SSH server and establish the SSH tunnel:
try {
await tunnel.sshOpenTunnelAsync(sshHostname, sshPort);
} catch {
console.log(tunnel.lastErrorText);
return;
}
// Authenticate with the SSH server via a login/password
// or with a public key.
// This example demonstrates SSH password authentication.
try {
await tunnel.sshAuthenticatePwAsync('mySshLogin', 'mySshPassword');
} catch {
console.log(tunnel.lastErrorText);
return;
}
// OK, the SSH tunnel is setup. Now open a channel within the tunnel.
// (Any number of channels may be created from the same SSH tunnel.
// Multiple channels may coexist at the same time.)
// This example connects to a REST server through the SSH tunnel.
// It will connect to the Amazon AWS service for this example.
const rest = new Rest();
const bTls = true;
const port = 443;
const maxWaitMs = 5000;
// This returns a socket object that is a single channel within the SSH tunnel.
const channel = new Socket();
try {
await tunnel.sshNewChannelAsync('s3.amazonaws.com', port, bTls, maxWaitMs, channel);
} catch {
console.log(tunnel.lastErrorText);
return;
}
// Use the connection. (This connection is a TLS running on an SSH channel through an SSH tunnel.
// In other words, TLS is wrapped within the SSH tunnel.)
try {
rest.useConnection(channel, true);
} catch {
console.log(rest.lastErrorText);
return;
}
// Provide AWS credentials for the REST call.
const authAws = new AuthAws();
authAws.accessKey = 'AWS_ACCESS_KEY';
authAws.secretKey = 'AWS_SECRET_KEY';
authAws.serviceName = 's3';
rest.setAuthAws(authAws);
// List all buckets for the account...
let responseXml: string;
try {
responseXml = await rest.fullRequestNoBodyAsync('GET', '/');
} catch {
console.log(rest.lastErrorText);
return;
}
const xml = new Xml();
xml.loadXml(responseXml);
// Show the full XML returned.
console.log(xml.getXml());
// Iterate over the buckets, showing each bucket name.
xml.findChild2('Buckets');
if (xml.firstChild2()) {
console.log(xml.getChildContent('Name'));
while (xml.nextSibling2()) {
console.log(xml.getChildContent('Name'));
}
}
// Move the internal pointer back to the root node.
xml.getRoot2();
}