React Native
React Native
Get a REST Response Header by Name
See more REST Examples
Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.
Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.
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;
}
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 a response header field by name. Header field names are case-insensitive.
let contentType: string;
try {
contentType = rest.responseHdrByName('Content-Type');
} catch {
console.log(rest.lastErrorText);
return;
}
console.log(`Content-Type: ${contentType}`);
}