Sample code for 30+ languages & platforms
React Native

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.

Chilkat React Native Downloads

React Native
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;
  }

  try {
    await rest.sendReqNoBodyAsync('GET', '/api/items/123');
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  const statusCode = await rest.readResponseHeaderAsync();
  if (statusCode < 0) {
    console.log(rest.lastErrorText);
    return;
  }

  // Return the value of the response header at a zero-based index.  Use the same index with
  // ResponseHdrName to get the corresponding field name.
  const numHeaders = rest.numResponseHeaders;
  for (let i = 0; i < numHeaders; i++) {
    let value: string;
    try {
      value = rest.responseHdrValue(i);
    } catch {
      console.log(rest.lastErrorText);
      return;
    }

    console.log(`Header ${i} value: ${value}`);
  }
}