React Native
React Native
Send a REST Request with a Binary Body
See more REST Examples
Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).
Chilkat React Native Downloads
import { BinData, Rest, StringBuilder } 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;
}
// The file paths are relative to the application's current working directory. Absolute paths
// may also be used. Supply the paths appropriate to your own environment.
// FullRequestBd sends a request whose binary body is read from a BinData and stores the text
// response body in a StringBuilder.
const bdRequest = new BinData();
try {
bdRequest.loadFile('qa_data/image.png');
} catch {
console.log('Failed to load the request body file.');
return;
}
rest.addHeader('Content-Type', 'image/png');
const sbResponse = new StringBuilder();
try {
await rest.fullRequestBdAsync('PUT', '/api/images/1', bdRequest, sbResponse);
} catch {
console.log(rest.lastErrorText);
return;
}
let responseText: string;
try {
responseText = sbResponse.getAsString();
} catch {
console.log(sbResponse.lastErrorText);
return;
}
console.log(responseText);
}