Java
Chilkat2 Java
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 Java Downloads
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" \
// -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"
// }
JsonObject 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");
HttpResponse resp = new HttpResponse();
try {
http.httpJson("POST", "https://api.harvestapp.com/v2/contacts", json, "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": 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
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");
}
}