Sanity Check code

Can someone sanity-check this for me please? Its pulls records from a CRM via API and filling them into a list box. I copied it from a PHP script that I had an tried to do the same thing in Xojo, but its not working as expected.

The PHP Code works fine.

//API credentials
Var apiUsername As String = "username here "
Var apiPassword As String = "Password here"
Var integrationCode As String = "Code here"

// Autotask API endpoint
Var apiEndpoint As String = ""

// Autotask API URL to retrieve list of companies
Var url As String = apiEndpoint + "/Companies/query?search={\"filter\":[{\"op\":\"eq\",\"field\":\"CompanyType\",\"value\":\"1\"}]}"

Var response As String =URLConnection1.Send("GET", url)

// Parse JSON response
Var json As New JSONItem(response)

//Set HTTP Request and get Response
URLConnection1.SetRequestContent("Content-type", "application/json')
URLConnection1.RequestHeader("ApiIntegrationCode", integrationCode)
URLConnection1.RequestHeader("UserName", apiUsername)
URLConnection1.RequestHeader("Secret", apiPassword)

// Extract company data from JSON response
Var companies() As Dictionary
For Each account As JSONItem In json.Value("items")
  Var company As New Dictionary
  company.Value("id") = account.Value("id").ToInteger
  company.Value("name") = account.Value("companyName").ToString
  company.Value("phone") = account.Value("phone").ToString
  company.Value("website") = account.Value("webAddress").ToString
  company.Value("city") = account.Value("city").ToString
  company.Value("state") = account.Value("state").ToString
  company.Value("country") = account.Value("countryID").ToString

// Display company data
For Each company As Dictionary In companies
  ListBox1.AddRow("ID: " + company.Value("id").ToString)
  ListBox1.AddRow("Name: " + company.Value("name").ToString)
  ListBox1.AddRow("Phone: " + company.Value("phone").ToString)
  ListBox1.AddRow("Website: " + company.Value("website").ToString)
  ListBox1.AddRow("City: " + company.Value("city").ToString)
  ListBox1.AddRow("State: " + company.Value("state").ToString)
  ListBox1.AddRowt("Country: " + company.Value("country").ToString)

I am also getting a Syntax error on this line so not sure what it should be

Var url As String = apiEndpoint + "/Companies/query?search={\"filter\":[{\"op\":\"eq\",\"field\":\"CompanyType\",\"value\":\"1\"}]}"

Here is the original PHP Code (which works perfectly fine)


// Autotask API credentials
$apiUsername = 'Username';
$apiPassword = 'Password';
$integrationCode = 'Code';

// Autotask API endpoint
$apiEndpoint = '';

// Autotask API URL to retrieve list of companies
$url = $apiEndpoint . '/Companies/query?search={"filter":[{"op":"eq","field":"CompanyType","value":"1"}]}';

// Set HTTP headers
$headers = array(
  'Content-Type: application/json',
  'ApiIntegrationCode: ' . $integrationCode,
  'UserName:' . $apiUsername,
  'Secret: ' . $apiPassword

// Set HTTP authentication credentials
$auth = array(

// Set HTTP context options
$options = array(
  'http' => array(
    'method' => 'GET',
    'header' => implode("\r\n", $headers),
    'auth' => implode(":", $auth),
    'ignore_errors' => true

// Create HTTP context
$context = stream_context_create($options);

// Send HTTP request and get response
$response = file_get_contents($url, false, $context);

// Parse JSON response
$json = json_decode($response, true);

// Check if JSON parsing succeeded
if (json_last_error() !== JSON_ERROR_NONE) {
   die('Error: Unable to parse JSON response');

// Extract company data from JSON response
$companies = array();
foreach ($json["items"] as $account) {	
  $company = array(
    'id' => (int) $account['id'],
    'name' => (string) $account['companyName'],
    'phone' => (string) $account['phone'],
    'website' => (string) $account['webAddress'],
    'city' => (string) $account['city'],
    'state' => (string) $account['state'],
    'country' => (string) $account['countryID']
  array_push($companies, $company);

// Display company data in a grid
echo '<table>';
echo '<tr><th>ID</th><th>Name</th><th>Phone</th><th>Website</th><th>City</th><th>State</th><th>Country</th></tr>';
foreach ($companies as $company) {
  echo '<tr>';
  echo '<td>' . $company['id'] . '</td>';
  echo '<td>' . $company['name'] . '</td>';
  echo '<td>' . $company['phone'] . '</td>';
  echo '<td>' . $company['website'] . '</td>';
  echo '<td>' . $company['city'] . '</td>';
  echo '<td>' . $company['state'] . '</td>';
  echo '<td>' . $company['country'] . '</td>';
  echo '</tr>';
echo '</table>';

I suspect this line attempting to embed " within the string. You can’t use " do do that in Xojo. Replace the " with “”.

Var url As String = apiEndpoint + "/Companies/query?search={""filter"":[{""op"":""eq"",""field"":""CompanyType"",""value"":""1""}]}"

There could be other issues.

Hi Chris,

One thing to consider is sending the response from URLConnection.ContentReceived to the debug console and visually accessing the response. There may be clues in there.

Thanks I will give it a go

Thanks. The problm is I am just getting all kinds of errors before the code even runs

Hs mismatched ". It starts with " and ends with ’ (’ isn’t allowed as a delimiter in Xojo).

Thanks Ian.

I am getting this error

There is more than one method with this name but this does not match any of the available signatures.
on this line

Var response As String =  URLConnection1.Send("GET", url)

What does that mean?

That means the Send Method has many definitions, but no one that have two parameters, the fisrt one being a String and the second one of url type.

The signature is the type returned and the types of the arguments.

That means that URLConnection does not have a method called Send that takes a String and a URL and returns a string. check the documentation for what is required:

Neither of the Send methods returns a string. It is asynchronous, meaning you call send and then an event fires to let you know that data is available. Look at the examples.

Url is a string

// Autotask API endpoint
Var apiEndpoint As String = ""

// Autotask API URL to retrieve list of companies
Var url As String = apiEndpoint + "/Companies/query?search={""filter"":[{""op"":""eq"",""field"":""CompanyType"",""value"":""1""}]}"

Var response As String =URLConnection1.Send("GET", url)

I declare the variable in the URL above.

In the example on the documentation it looks like its doing exactly what I am doing

mySocket.Send(“GET”, “”)

It seems to me that you are setting your request headers after you actually send the request… you need to set them before you “.send” the request.

Send does not return a string. You cannot use it that way.

Would you mind helping me with how I should use it then ?

Send can return a string, but you need to give it a third parameter… the timeout.

Var response As String =URLConnection1.Send("GET", url, 30)

Your PHP code is calling the connection synchronously. Code execution stops until it gets a response. URLConnection.Send operates asynchronously. While async is the preferred approach, perhaps you should use SendSynch instead, in order to emulate what PHP is doing. At least until you get it working.

Have you looked at the Xojo Examples in Examples - Communication - Internet - Web Services, maybe one of those will help you on what you are trying to do.

@Greg_O, I think you mean SendSynch. Send does not return a value.

Nope tried that . exactly the same thing

Yes but I think i am using it wrong (so maybe just dont understand what its doing)

Would someone mind helping me with actually how this should work I have been going back and forth all night looking at docs and I think I have it right so obviously not understanding it correctly

I try and make the changes that everyone recommends but just get more and more errors. I am new to programming and just want to ty and learn but getting frustrated that I am doing what I think is right and nothing is working