Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
-- Use this online tool to generate code from sample JSON: Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "name": "Kevin's Corral",
-- "date_format": "mm/dd/yyyy",
-- "address_attributes": {
-- "country": "Canada"
-- },
-- "phone_number_attributes": null
-- }
DECLARE @json int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'Kevin''s Corral'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'date_format', 'mm/dd/yyyy'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'address_attributes.country', 'Canada'
EXEC sp_OAMethod @json, 'UpdateNull', @success OUT, 'phone_number_attributes'
-- Adds the "Authorization: Bearer <access_token>" header.
EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
DECLARE @resp int
EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://api.freshbooks.com/auth/api/v1/users/business', 'application/json', @json
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
RETURN
END
DECLARE @sbResponseBody int
-- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT
EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
DECLARE @jResp int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
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 @resp, 'StatusCode', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode >= 400
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
RETURN
END
EXEC @hr = sp_OADestroy @resp
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "response": {
-- "id": 1986590,
-- "name": "Kevin's Corral",
-- "account_id": "lwy0vG",
-- "business_group": {
-- "id": 4977602,
-- "category": "business",
-- "members": [
-- {
-- "id": 6487912,
-- "group_id": 4977602,
-- "role": "owner",
-- "identity_id": 37256,
-- "first_name": "Marshall",
-- "last_name": "Johnston",
-- "email": "api.freshbooks@gmail.com",
-- "company": "Kevin's Corral",
-- "business_id": 1986590,
-- "unacknowledged_change": false,
-- "active": true
-- }
-- ]
-- },
-- "date_format": "mm/dd/yyyy",
-- "address": {
-- "id": 2448988,
-- "street": null,
-- "city": null,
-- "province": null,
-- "country": "Canada",
-- "postal_code": null
-- },
-- "phone_number": null,
-- "business_clients": [
-- ]
-- }
-- }
-- Sample code for parsing the JSON response...
-- Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
DECLARE @Date_format int
-- Use "Chilkat_9_5_0.DtObj" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.DtObj', @Date_format OUT
DECLARE @id int
DECLARE @group_id int
DECLARE @role nvarchar(4000)
DECLARE @identity_id int
DECLARE @first_name nvarchar(4000)
DECLARE @last_name nvarchar(4000)
DECLARE @email nvarchar(4000)
DECLARE @company nvarchar(4000)
DECLARE @business_id int
DECLARE @unacknowledged_change int
DECLARE @active int
DECLARE @Id int
EXEC sp_OAMethod @jResp, 'IntOf', @Id OUT, 'response.id'
DECLARE @Name nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'response.name'
DECLARE @Account_id nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Account_id OUT, 'response.account_id'
DECLARE @Business_groupId int
EXEC sp_OAMethod @jResp, 'IntOf', @Business_groupId OUT, 'response.business_group.id'
DECLARE @Category nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Category OUT, 'response.business_group.category'
EXEC sp_OAMethod @jResp, 'DtOf', @success OUT, 'response.date_format', 0, @Date_format
DECLARE @AddressId int
EXEC sp_OAMethod @jResp, 'IntOf', @AddressId OUT, 'response.address.id'
DECLARE @Street nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Street OUT, 'response.address.street'
DECLARE @City nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @City OUT, 'response.address.city'
DECLARE @Province nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Province OUT, 'response.address.province'
DECLARE @Country nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Country OUT, 'response.address.country'
DECLARE @Postal_code nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Postal_code OUT, 'response.address.postal_code'
DECLARE @Phone_number nvarchar(4000)
EXEC sp_OAMethod @jResp, 'StringOf', @Phone_number OUT, 'response.phone_number'
DECLARE @i int
SELECT @i = 0
DECLARE @count_i int
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.business_group.members'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
EXEC sp_OAMethod @jResp, 'IntOf', @id OUT, 'response.business_group.members[i].id'
EXEC sp_OAMethod @jResp, 'IntOf', @group_id OUT, 'response.business_group.members[i].group_id'
EXEC sp_OAMethod @jResp, 'StringOf', @role OUT, 'response.business_group.members[i].role'
EXEC sp_OAMethod @jResp, 'IntOf', @identity_id OUT, 'response.business_group.members[i].identity_id'
EXEC sp_OAMethod @jResp, 'StringOf', @first_name OUT, 'response.business_group.members[i].first_name'
EXEC sp_OAMethod @jResp, 'StringOf', @last_name OUT, 'response.business_group.members[i].last_name'
EXEC sp_OAMethod @jResp, 'StringOf', @email OUT, 'response.business_group.members[i].email'
EXEC sp_OAMethod @jResp, 'StringOf', @company OUT, 'response.business_group.members[i].company'
EXEC sp_OAMethod @jResp, 'IntOf', @business_id OUT, 'response.business_group.members[i].business_id'
EXEC sp_OAMethod @jResp, 'BoolOf', @unacknowledged_change OUT, 'response.business_group.members[i].unacknowledged_change'
EXEC sp_OAMethod @jResp, 'BoolOf', @active OUT, 'response.business_group.members[i].active'
SELECT @i = @i + 1
END
SELECT @i = 0
EXEC sp_OAMethod @jResp, 'SizeOfArray', @count_i OUT, 'response.business_clients'
WHILE @i < @count_i
BEGIN
EXEC sp_OASetProperty @jResp, 'I', @i
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbResponseBody
EXEC @hr = sp_OADestroy @jResp
EXEC @hr = sp_OADestroy @Date_format
END
GO
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-H "Content-Type: application/json"
-d '{
"name": "Kevin\'s Corral",
"date_format": "mm/dd/yyyy",
"address_attributes": {
"country": "Canada"
},
"phone_number_attributes": null
}'
https://api.freshbooks.com/auth/api/v1/users/business
Postman Collection Item JSON
{
"name": "Add Business",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"Kevin's Corral\",\n \"date_format\": \"mm/dd/yyyy\",\n \"address_attributes\": {\n \"country\": \"Canada\"\n },\n \"phone_number_attributes\": null\n}"
},
"url": {
"raw": "https://api.freshbooks.com/auth/api/v1/users/business",
"protocol": "https",
"host": [
"api",
"freshbooks",
"com"
],
"path": [
"auth",
"api",
"v1",
"users",
"business"
]
}
},
"response": [
{
"name": "Add Business",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"Kevin's Corral\",\n \"date_format\": \"mm/dd/yyyy\",\n \"address_attributes\": {\n \"country\": \"Canada\"\n },\n \"phone_number_attributes\": null\n}"
},
"url": {
"raw": "https://my.freshbooks.com/service/api/auth/api/v1/users/business",
"protocol": "https",
"host": [
"my",
"freshbooks",
"com"
],
"path": [
"service",
"api",
"auth",
"api",
"v1",
"users",
"business"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Server",
"value": "nginx"
},
{
"key": "Content-Type",
"value": "application/json; charset=utf-8"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-Frame-Options",
"value": "SAMEORIGIN"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-Download-Options",
"value": "noopen"
},
{
"key": "X-Permitted-Cross-Domain-Policies",
"value": "none"
},
{
"key": "Referrer-Policy",
"value": "strict-origin-when-cross-origin"
},
{
"key": "ETag",
"value": "W/\"ece9403d440d4badd777fca953dd9580\""
},
{
"key": "Cache-Control",
"value": "no-cache"
},
{
"key": "X-Request-Id",
"value": "49e5ba95-b7dc-42c7-b0c3-8d61d99d1f39"
},
{
"key": "X-Runtime",
"value": "0.670145"
},
{
"key": "X-NewRelic-App-Data",
"value": "PxQBWV5TCBABVlVSBAUHUFQTGhEhCQ0WQg1UDl1KG39aAV0NVBZSEAEZZQAFCBdyUxAIDVl1XhUUUEJQClxZMF8XTAtWXw4IYQpMFVQyVhZLVVYNCkEUAx5UTVIZBwBXVgUEBFNVWlAIUApQDxQZAx9HDlIAVQpQBQBUV1AFXFUBXUM/"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Expires",
"value": "Wed, 25 Apr 2018 16:51:20 GMT"
},
{
"key": "Content-Encoding",
"value": "gzip"
},
{
"key": "Via",
"value": "1.1 google"
},
{
"key": "Via",
"value": "1.1 varnish"
},
{
"key": "Transfer-Encoding",
"value": "chunked"
},
{
"key": "Accept-Ranges",
"value": "bytes"
},
{
"key": "Date",
"value": "Thu, 25 Apr 2019 16:51:20 GMT"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "X-Served-By",
"value": "cache-mdw17366-MDW"
},
{
"key": "X-Cache",
"value": "MISS"
},
{
"key": "X-Cache-Hits",
"value": "0"
},
{
"key": "Vary",
"value": "Accept-Encoding"
},
{
"key": "Country",
"value": "CA"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=31536000; includeSubDomains; preload"
}
],
"cookie": [
],
"body": "{\n \"response\": {\n \"id\": 1986590,\n \"name\": \"Kevin's Corral\",\n \"account_id\": \"lwy0vG\",\n \"business_group\": {\n \"id\": 4977602,\n \"category\": \"business\",\n \"members\": [\n {\n \"id\": 6487912,\n \"group_id\": 4977602,\n \"role\": \"owner\",\n \"identity_id\": 37256,\n \"first_name\": \"Marshall\",\n \"last_name\": \"Johnston\",\n \"email\": \"api.freshbooks@gmail.com\",\n \"company\": \"Kevin's Corral\",\n \"business_id\": 1986590,\n \"unacknowledged_change\": false,\n \"active\": true\n }\n ]\n },\n \"date_format\": \"mm/dd/yyyy\",\n \"address\": {\n \"id\": 2448988,\n \"street\": null,\n \"city\": null,\n \"province\": null,\n \"country\": \"Canada\",\n \"postal_code\": null\n },\n \"phone_number\": null,\n \"business_clients\": []\n }\n}"
}
]
}