Sample code for 30+ languages & platforms
SQL Server

GetHarvest - List Clients

See more GetHarvest Examples

Returns a list of your clients.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  This example assumes the Chilkat API to have been previously unlocked.
    --  See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Implements the following CURL command:

    --  curl "https://api.harvestapp.com/v2/clients" \
    --    -H "Authorization: Bearer ACCESS_TOKEN" \
    --    -H "Harvest-Account-Id: ACCOUNT_ID" \
    --    -H "User-Agent: MyApp (yourname@example.com)"

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'User-Agent', 'MyApp (yourname@example.com)'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Authorization', 'Bearer ACCESS_TOKEN'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Harvest-Account-Id', 'ACCOUNT_ID'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @http, 'QuickGetSb', @success OUT, 'https://api.harvestapp.com/v2/clients', @sbResponseBody
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    --  Sample JSON response:

    --  {
    --    "clients": [
    --      {
    --        "id": 5735776,
    --        "name": "123 Industries",
    --        "is_active": true,
    --        "address": "123 Main St.\r\nAnytown, LA 71223",
    --        "created_at": "2017-06-26T21:02:12Z",
    --        "updated_at": "2017-06-26T21:34:11Z",
    --        "currency": "EUR"
    --      },
    --      {
    --        "id": 5735774,
    --        "name": "ABC Corp",
    --        "is_active": true,
    --        "address": "456 Main St.\r\nAnytown, CT 06467",
    --        "created_at": "2017-06-26T21:01:52Z",
    --        "updated_at": "2017-06-26T21:27:07Z",
    --        "currency": "USD"
    --      }
    --    ],
    --    "per_page": 100,
    --    "total_pages": 1,
    --    "total_entries": 2,
    --    "next_page": null,
    --    "previous_page": null,
    --    "page": 1,
    --    "links": {
    --      "first": "https://api.harvestapp.com/v2/clients?page=1&per_page=100",
    --      "next": null,
    --      "previous": null,
    --      "last": "https://api.harvestapp.com/v2/clients?page=1&per_page=100"
    --    }
    --  }

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

    DECLARE @per_page int

    DECLARE @total_pages int

    DECLARE @total_entries int

    DECLARE @next_page nvarchar(4000)

    DECLARE @previous_page nvarchar(4000)

    DECLARE @page int

    DECLARE @linksFirst nvarchar(4000)

    DECLARE @linksNext nvarchar(4000)

    DECLARE @linksPrevious nvarchar(4000)

    DECLARE @linksLast nvarchar(4000)

    DECLARE @i int

    DECLARE @count_i int

    DECLARE @id int

    DECLARE @name nvarchar(4000)

    DECLARE @is_active int

    DECLARE @address nvarchar(4000)

    DECLARE @created_at nvarchar(4000)

    DECLARE @updated_at nvarchar(4000)

    DECLARE @currency nvarchar(4000)

    EXEC sp_OAMethod @jResp, 'IntOf', @per_page OUT, 'per_page'
    EXEC sp_OAMethod @jResp, 'IntOf', @total_pages OUT, 'total_pages'
    EXEC sp_OAMethod @jResp, 'IntOf', @total_entries OUT, 'total_entries'
    EXEC sp_OAMethod @jResp, 'StringOf', @next_page OUT, 'next_page'
    EXEC sp_OAMethod @jResp, 'StringOf', @previous_page OUT, 'previous_page'
    EXEC sp_OAMethod @jResp, 'IntOf', @page OUT, 'page'
    EXEC sp_OAMethod @jResp, 'StringOf', @linksFirst OUT, 'links.first'
    EXEC sp_OAMethod @jResp, 'StringOf', @linksNext OUT, 'links.next'
    EXEC sp_OAMethod @jResp, 'StringOf', @linksPrevious OUT, 'links.previous'
    EXEC sp_OAMethod @jResp, 'StringOf', @linksLast OUT, 'links.last'
    SELECT @i = 0
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'clients'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jResp, 'I', @i
        EXEC sp_OAMethod @jResp, 'IntOf', @id OUT, 'clients[i].id'
        EXEC sp_OAMethod @jResp, 'StringOf', @name OUT, 'clients[i].name'
        EXEC sp_OAMethod @jResp, 'BoolOf', @is_active OUT, 'clients[i].is_active'
        EXEC sp_OAMethod @jResp, 'StringOf', @address OUT, 'clients[i].address'
        EXEC sp_OAMethod @jResp, 'StringOf', @created_at OUT, 'clients[i].created_at'
        EXEC sp_OAMethod @jResp, 'StringOf', @updated_at OUT, 'clients[i].updated_at'
        EXEC sp_OAMethod @jResp, 'StringOf', @currency OUT, 'clients[i].currency'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO