Sample code for 30+ languages & platforms
Dart

GetHarvest - Update Contact

See more GetHarvest Examples

Updates the specific contact by setting the values of the parameters passed. Any parameters not provided will be left unchanged. Returns a contact object and a 200 OK response code if the call succeeded.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final http = CkHttp();

  // Implements the following CURL command:

  // curl "https://api.harvestapp.com/v2/contacts/CONTACT_ID" \
  //   -H "Authorization: Bearer ACCESS_TOKEN" \
  //   -H "Harvest-Account-Id: ACCOUNT_ID" \
  //   -H "User-Agent: MyApp (yourname@example.com)" \
  //   -X PATCH \
  //   -H "Content-Type: application/json" \
  //   -d '{"title":"Owner"}'

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

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

  // {
  //   "title": "Owner"
  // }

  final json = CkJsonObject();
  json.updateString('title', 'Owner');

  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');

  final sbRequestBody = CkStringBuilder();
  json.emitSb(sbRequestBody);

  final resp = CkHttpResponse();
  try {
    http.httpSb('PATCH', 'https://api.harvestapp.com/v2/contacts/CONTACT_ID', sbRequestBody, 'utf-8', 'application/json', resp);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sbResponseBody = CkStringBuilder();
  resp.getBodySb(sbResponseBody);
  final jResp = CkJsonObject();
  jResp.loadSb(sbResponseBody);
  jResp.emitCompact = false;

  print('Response Body:');
  print(jResp.emit());

  final respStatusCode = resp.statusCode;
  print('Response Status Code = $respStatusCode');
  if (respStatusCode >= 400) {
    print('Response Header:');
    print(resp.header);
    print('Failed.');
    return;
  }

  // Sample JSON response:

  // {
  //   "id": 4706510,
  //   "title": "Owner",
  //   "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:46:48Z",
  //   "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

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