Sample code for 30+ languages & platforms
React Native

GetHarvest - Create Contact

See more GetHarvest Examples

Creates a new contact object. Returns a contact object and a 201 Created response code if the call succeeded.

Chilkat React Native Downloads

React Native
import { Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const http = new Http();

  // Implements the following CURL command:

  // curl "https://api.harvestapp.com/v2/contacts" \
  //   -H "Authorization: Bearer ACCESS_TOKEN" \
  //   -H "Harvest-Account-Id: ACCOUNT_ID" \
  //   -H "User-Agent: MyApp (yourname@example.com)" \
  //   -X POST \
  //   -H "Content-Type: application/json" \
  //   -d '{"client_id":8282839,"first_name":"George","last_name":"Frank","email":"georgefrank@example.com"}'

  // Use this online tool to generate code from sample JSON:
  // Generate Code to Create JSON

  // The following JSON is sent in the request body.

  // {
  //   "client_id": 8282839,
  //   "first_name": "George",
  //   "last_name": "Frank",
  //   "email": "georgefrank@example.com"
  // }

  const json = new JsonObject();
  json.updateInt('client_id', 8282839);
  json.updateString('first_name', 'George');
  json.updateString('last_name', 'Frank');
  json.updateString('email', 'georgefrank@example.com');

  http.setRequestHeader('User-Agent', 'MyApp (yourname@example.com)');
  http.setRequestHeader('Content-Type', 'application/json');
  http.setRequestHeader('Authorization', 'Bearer ACCESS_TOKEN');
  http.setRequestHeader('Harvest-Account-Id', 'ACCOUNT_ID');

  const resp = new HttpResponse();
  try {
    await http.httpJsonAsync('POST', 'https://api.harvestapp.com/v2/contacts', json, 'application/json', resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  const sbResponseBody = new StringBuilder();
  resp.getBodySb(sbResponseBody);
  const jResp = new JsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  console.log('Response Body:');
  console.log(jResp.emit());

  const respStatusCode = resp.statusCode;
  console.log(`Response Status Code = ${respStatusCode}`);
  if (respStatusCode >= 400) {
    console.log('Response Header:');
    console.log(resp.header);
    console.log('Failed.');
    return;
  }

  // Sample JSON response:

  // {
  //   "id": 4706510,
  //   "title": null,
  //   "first_name": "George",
  //   "last_name": "Frank",
  //   "email": "georgefrank@example.com",
  //   "phone_office": "",
  //   "phone_mobile": "",
  //   "fax": "",
  //   "created_at": "2017-06-26T21:44:57Z",
  //   "updated_at": "2017-06-26T21:44:57Z",
  //   "client": {
  //     "id": 5735776,
  //     "name": "123 Industries"
  //   }
  // }

  // Sample code for parsing the JSON response...
  // Use the following online tool to generate parsing code from sample JSON:
  // Generate Parsing Code from JSON

  const id = jResp.intOf('id');
  const title = jResp.stringOf('title');
  const firstName = jResp.stringOf('first_name');
  const lastName = jResp.stringOf('last_name');
  const email = jResp.stringOf('email');
  const phoneOffice = jResp.stringOf('phone_office');
  const phoneMobile = jResp.stringOf('phone_mobile');
  const fax = jResp.stringOf('fax');
  const createdAt = jResp.stringOf('created_at');
  const updatedAt = jResp.stringOf('updated_at');
  const clientId = jResp.intOf('client.id');
  const clientName = jResp.stringOf('client.name');
}