Sample code for 30+ languages & platforms
React Native

Explaining the Rest ClearResponseBodyStream Method

See more Azure Cloud Storage Examples

The ClearResponseBodyStream method would be needed if your applicaiton called SetResponseBodyStream to send the response to a stream for one request, but then wants to subsequently do something else. The application must call ClearResponseBodyStream to no longer send responses to the stream.

Chilkat React Native Downloads

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

async function chilkatExample() {
  let success = false;

  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const rest = new Rest();

  // Connect to the web server
  const bTls = true;
  const port = 443;
  const bAutoReconnect = true;
  try {
    await rest.connectAsync('www.chilkatsoft.com', port, bTls, bAutoReconnect);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  // Setup a file stream for the download
  const fileStream = new Stream();
  fileStream.sinkFile = 'qa_output/starfish.jpg';

  // Indicate that the call to FullRequestNoBody should send the response body
  // to fileStream if the response status code is 200.
  // If a non-success response status code is received, then nothing
  // is streamed to the output file and the error response is returned by FullRequestNoBody.
  const expectedStatus = 200;
  rest.setResponseBodyStream(expectedStatus, true, fileStream);

  let responseStr = '';
  try {
    responseStr = await rest.fullRequestNoBodyAsync('GET', '/images/starfish.jpg');
  } catch {
    // Examine the request/response to see what happened.
    console.log(`response status code = ${rest.responseStatusCode}`);
    console.log(`response status text = ${rest.responseStatusText}`);
    console.log(`response header: ${rest.responseHeader}`);
    console.log(`response body (if any): ${responseStr}`);
    console.log('---');
    console.log(`LastRequestStartLine: ${rest.lastRequestStartLine}`);
    console.log(`LastRequestHeader: ${rest.lastRequestHeader}`);
    return;
  }

  console.log('downloaded starfish.jpg');

  // Clear the response body stream previously set in the call to SetResponseBodyStream.
  rest.clearResponseBodyStream();

  // Download something else, but this time send the response body to bd.
  const bd = new BinData();
  try {
    await rest.fullRequestNoBodyBdAsync('GET', '/images/penguins.jpg', bd);
  } catch {
    // Examine the request/response to see what happened.
    console.log(`response status code = ${rest.responseStatusCode}`);
    console.log(`response status text = ${rest.responseStatusText}`);
    console.log(`response header: ${rest.responseHeader}`);
    console.log(`response body (if any): ${bd.getString('utf-8')}`);
    console.log('---');
    console.log(`LastRequestStartLine: ${rest.lastRequestStartLine}`);
    console.log(`LastRequestHeader: ${rest.lastRequestHeader}`);
    return;
  }

  // Save the contents of bd to a file.
  success = true;
  try {
    bd.writeFile('qa_output/penguins.jpg');
  } catch {
    success = false;
  }

  console.log(`Success: ${success}`);
}