Sample code for 30+ languages & platforms
React Native

Send a REST Request with a Text Body

See more REST Examples

Demonstrates Rest.FullRequestString, a convenience method that sends a complete request whose text body (such as JSON, XML, or plain text) is supplied directly, then reads and returns the response body as text.

Background. The full-request convenience methods perform the entire request/response exchange in a single call. FullRequestString is well suited to typical JSON or XML API calls where the body fits comfortably in a string.

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

  // Send a complete request whose text body is supplied directly, such as JSON.  The response body is
  // read as text and returned.  The 1st argument is the HTTP verb, the 2nd is the URI path, and the
  // 3rd is the request body.
  rest.addHeader('Content-Type', 'application/json');

  const jsonBody = '{ "name": "example", "active": true }';
  let responseText: string;
  try {
    responseText = await rest.fullRequestStringAsync('POST', '/api/items', jsonBody);
  } catch {
    console.log(rest.lastErrorText);
    return;
  }

  console.log(responseText);
}