Sample code for 30+ languages & platforms
React Native

Enumerate REST Response Header Names

See more REST Examples

Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.

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

  // Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
  // and ResponseHdrValue returns the value at the same index.
  const numHeaders = rest.numResponseHeaders;
  for (let i = 0; i < numHeaders; i++) {
    let name: string;
    try {
      name = rest.responseHdrName(i);
    } catch {
      console.log(rest.lastErrorText);
      return;
    }

    let value: string;
    try {
      value = rest.responseHdrValue(i);
    } catch {
      console.log(rest.lastErrorText);
      return;
    }

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