Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) GetHarvest - Create ContactCreates a new contact object. Returns a contact object and a 201 Created response code if the call succeeded. For more information, see https://help.getharvest.com/api-v2/clients-api/clients/contacts/
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> #include <C_CkStringBuilderW.h> void ChilkatSample(void) { HCkHttpW http; BOOL success; HCkJsonObjectW json; HCkHttpResponseW resp; HCkStringBuilderW sbResponseBody; HCkJsonObjectW jResp; int respStatusCode; int id; const wchar_t *title; const wchar_t *first_name; const wchar_t *last_name; const wchar_t *email; const wchar_t *phone_office; const wchar_t *phone_mobile; const wchar_t *fax; const wchar_t *created_at; const wchar_t *updated_at; int clientId; const wchar_t *clientName; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // 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" // } json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateInt(json,L"client_id",8282839); CkJsonObjectW_UpdateString(json,L"first_name",L"George"); CkJsonObjectW_UpdateString(json,L"last_name",L"Frank"); CkJsonObjectW_UpdateString(json,L"email",L"georgefrank@example.com"); CkHttpW_SetRequestHeader(http,L"User-Agent",L"MyApp (yourname@example.com)"); CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json"); CkHttpW_SetRequestHeader(http,L"Authorization",L"Bearer ACCESS_TOKEN"); CkHttpW_SetRequestHeader(http,L"Harvest-Account-Id",L"ACCOUNT_ID"); resp = CkHttpW_PostJson3(http,L"https://api.harvestapp.com/v2/contacts",L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); return; } sbResponseBody = CkStringBuilderW_Create(); CkHttpResponseW_GetBodySb(resp,sbResponseBody); jResp = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jResp,sbResponseBody); CkJsonObjectW_putEmitCompact(jResp,FALSE); wprintf(L"Response Body:\n"); wprintf(L"%s\n",CkJsonObjectW_emit(jResp)); respStatusCode = CkHttpResponseW_getStatusCode(resp); wprintf(L"Response Status Code = %d\n",respStatusCode); if (respStatusCode >= 400) { wprintf(L"Response Header:\n"); wprintf(L"%s\n",CkHttpResponseW_header(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); return; } CkHttpResponseW_Dispose(resp); // 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 id = CkJsonObjectW_IntOf(jResp,L"id"); title = CkJsonObjectW_stringOf(jResp,L"title"); first_name = CkJsonObjectW_stringOf(jResp,L"first_name"); last_name = CkJsonObjectW_stringOf(jResp,L"last_name"); email = CkJsonObjectW_stringOf(jResp,L"email"); phone_office = CkJsonObjectW_stringOf(jResp,L"phone_office"); phone_mobile = CkJsonObjectW_stringOf(jResp,L"phone_mobile"); fax = CkJsonObjectW_stringOf(jResp,L"fax"); created_at = CkJsonObjectW_stringOf(jResp,L"created_at"); updated_at = CkJsonObjectW_stringOf(jResp,L"updated_at"); clientId = CkJsonObjectW_IntOf(jResp,L"client.id"); clientName = CkJsonObjectW_stringOf(jResp,L"client.name"); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sbResponseBody); CkJsonObjectW_Dispose(jResp); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.