Sample code for 30+ languages & platforms
Java

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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

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

        Http http = new Http();

        // 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"
        // }

        JsonObject json = new JsonObject();
        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");

        StringBuilder sbRequestBody = new StringBuilder();
        json.emitSb(sbRequestBody);

        HttpResponse resp = new HttpResponse();
        try {
            http.httpSb("PATCH", "https://api.harvestapp.com/v2/contacts/CONTACT_ID", sbRequestBody, "utf-8", "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

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

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.println("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

        int id = jResp.intOf("id");
        String title = jResp.stringOf("title");
        String first_name = jResp.stringOf("first_name");
        String last_name = jResp.stringOf("last_name");
        String email = jResp.stringOf("email");
        String phone_office = jResp.stringOf("phone_office");
        String phone_mobile = jResp.stringOf("phone_mobile");
        String fax = jResp.stringOf("fax");
        String created_at = jResp.stringOf("created_at");
        String updated_at = jResp.stringOf("updated_at");
        int clientId = jResp.intOf("client.id");
        String clientName = jResp.stringOf("client.name");
    }
}