Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
IntakeQ: Send a Questionnaire
See more IntakeQ Examples
Send an intake package using the API.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Http,
Chilkat.StringBuilder,
Chilkat.HttpResponse,
Chilkat.JsonObject;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
json: TJsonObject;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
Text: string;
Answer: string;
QuestionType: string;
OfficeUse: Boolean;
OfficeNote: string;
j: Integer;
count_j: Integer;
Name: string;
DocumentType: string;
Signed: Boolean;
Id: string;
ClientName: string;
ClientEmail: string;
ClientId: Integer;
Status: string;
DateCreated: Integer;
DateSubmitted: Integer;
QuestionnaireName: string;
Practitioner: string;
PractitionerName: string;
AppointmentId: string;
i: Integer;
count_i: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// Implements the following CURL command:
// curl -H "Content-Type: application/json" \
// -H 'X-Auth-Key: xxxxxxxxxxxxxxxxxxxxxxxxx' \
// -X POST \
// -d '{
// "QuestionnaireId": "00000000-0000-0000-0000-000000000000",
// "ClientId": 123,
// "ClientName": "Dexter Morgan",
// "ClientEmail": "dexter@email.com",
// "ClientPhone": "2222222222",
// "PractitionerId": "00000000-0000-0000-0000-000000000000",
// }' https://intakeq.com/api/v1/intakes/send
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "QuestionnaireId": "00000000-0000-0000-0000-000000000000",
// "ClientId": 123,
// "ClientName": "Dexter Morgan",
// "ClientEmail": "dexter@email.com",
// "ClientPhone": "2222222222",
// "PractitionerId": "00000000-0000-0000-0000-000000000000"
// }
json := TJsonObject.Create;
json.UpdateString('QuestionnaireId','00000000-0000-0000-0000-000000000000');
json.UpdateInt('ClientId',123);
json.UpdateString('ClientName','Dexter Morgan');
json.UpdateString('ClientEmail','dexter@email.com');
json.UpdateString('ClientPhone','2222222222');
json.UpdateString('PractitionerId','00000000-0000-0000-0000-000000000000');
http.SetRequestHeader('Content-Type','application/json');
http.SetRequestHeader('X-Auth-Key','xxxxxxxxxxxxxxxxxxxxxxxxx');
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://intakeq.com/api/v1/intakes/send',json,'application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
sbResponseBody := TStringBuilder.Create;
resp.GetBodySb(sbResponseBody);
jResp := TJsonObject.Create;
jResp.LoadSb(sbResponseBody);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
end;
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "Id": "00000000-0000-0000-0000-000000000000",
// "ClientName": "test",
// "ClientEmail": "test@email.com",
// "ClientId": 9999,
// "Status": "Completed",
// "DateCreated": 1458526480368,
// "DateSubmitted": 1458526532654,
// "QuestionnaireName": "test",
// "Practitioner": "test@email.com",
// "PractitionerName": "FirstName LastName",
// "Questions": [
// {
// "Id": "xxxx-1",
// "Text": "Full name",
// "Answer": "Dexter Morgan",
// "QuestionType": "OpenQuestion",
// "Rows": [
// ],
// "ColumnNames": [
// ],
// "OfficeUse": false,
// "OfficeNote": "aaa"
// },
// {
// "Id": "xxxx-2",
// "Text": "Full name",
// "Answer": "Dexter Morgan",
// "QuestionType": "OpenQuestion",
// "Rows": [
// ],
// "ColumnNames": [
// ],
// "OfficeUse": false,
// "OfficeNote": "aaa"
// }
// ],
// "AppointmentId": "xxxxxxxx",
// "ConsentForms": [
// {
// "Id": "00000000-0000-0000-0000-000000000000",
// "Name": "HIPAA Release Form",
// "DocumentType": "Html",
// "Signed": true,
// "DateSubmitted": 1458526532654
// }
// ]
// }
// 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 := jResp.StringOf('Id');
ClientName := jResp.StringOf('ClientName');
ClientEmail := jResp.StringOf('ClientEmail');
ClientId := jResp.IntOf('ClientId');
Status := jResp.StringOf('Status');
DateCreated := jResp.IntOf('DateCreated');
DateSubmitted := jResp.IntOf('DateSubmitted');
QuestionnaireName := jResp.StringOf('QuestionnaireName');
Practitioner := jResp.StringOf('Practitioner');
PractitionerName := jResp.StringOf('PractitionerName');
AppointmentId := jResp.StringOf('AppointmentId');
i := 0;
count_i := jResp.SizeOfArray('Questions');
while i < count_i do
begin
jResp.I := i;
Id := jResp.StringOf('Questions[i].Id');
Text := jResp.StringOf('Questions[i].Text');
Answer := jResp.StringOf('Questions[i].Answer');
QuestionType := jResp.StringOf('Questions[i].QuestionType');
OfficeUse := jResp.BoolOf('Questions[i].OfficeUse');
OfficeNote := jResp.StringOf('Questions[i].OfficeNote');
j := 0;
count_j := jResp.SizeOfArray('Questions[i].Rows');
while j < count_j do
begin
jResp.J := j;
j := j + 1;
end;
j := 0;
count_j := jResp.SizeOfArray('Questions[i].ColumnNames');
while j < count_j do
begin
jResp.J := j;
j := j + 1;
end;
i := i + 1;
end;
i := 0;
count_i := jResp.SizeOfArray('ConsentForms');
while i < count_i do
begin
jResp.I := i;
Id := jResp.StringOf('ConsentForms[i].Id');
Name := jResp.StringOf('ConsentForms[i].Name');
DocumentType := jResp.StringOf('ConsentForms[i].DocumentType');
Signed := jResp.BoolOf('ConsentForms[i].Signed');
DateSubmitted := jResp.IntOf('ConsentForms[i].DateSubmitted');
i := i + 1;
end;
http.Free;
json.Free;
resp.Free;
sbResponseBody.Free;
jResp.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.