Getting Syntax Error

From the documentation:
https://documentation.xojo.com/api/networking/urlconnection.html#urlconnection-requestheader

.RequestHeader("Authorization") = "MyAPIKey"

maybe try:

request.RequestHeader("ApiIntegrationCode") = integrationCode
request.RequestHeader("UserName") = apiUsername
request.RequestHeader("Secret") = apiPassword

To break it down a little

RequestHeader(name As String, Assigns value As String)

The Assigns keyword means that you can treat it as a assignment using “=”. Eg.,

request.RequestHeader("name") = "value"

like Alberto pointed out. I only reiterate it to point out the meaning of “Assigns” when you run across it in the documentation. You will see it a lot.

2 Likes

Maybe this will help: (I note Xojo reposted a link via Twitter last night, so maybe its a hint)

Very interesting thank you. I will have a read

hey all. Firstly thank you for everyone who has stuck with me while I am correcting my code. I am getting there slowly but surely



Var  apiUsername As String = "Username"
Var  apiPassword As String = "Password"
Var  integrationCode As String = "Key"

Var apiEndpoint As String = "https://webservices16.autotask.net/atservicesrest/V1.0/Companies"





If TextField1.Text = "" Or TextField2.Text = "" Or TextField3.Text = "" Or TextField4.Text = "" Or TextField5.Text = "" Or TextField6.Text = "" Then
  MsgBox("Please fill in all fields.")
  Return
End If


Var  requestBody as New JSONItem
requestBody.Value("companyType") = 1
requestBody.Value("ownerResourceID") = 29682885
requestBody.Value("companyName") = TextField1.Text
requestBody.Value("Phone") = TextField2.Text
requestBody.Value("WebAddress") = TextField3.Text
requestBody.Value("City") = TextField4.Text
requestBody.Value("State") = TextField5.Text
requestBody.Value("CountryID") = TextField6.Text


Var request As New URLConnection
request.SetRequestContent(requestBody.ToString, "application/json")
request.RequestHeader("ApiIntegrationCode") = integrationCode
request.RequestHeader("UserName") = apiUsername
request.RequestHeader("Secret") = apiPassword

'request.send("POST",apiEndpoint)

request.send("POST", apiEndpoint, HandleResponse)

Now that I have corrected the code, it runs (without error) but nothing happens on the other end, ie the company is not created in the CRM .

This is the code I am using and all runs without error.

request.send("POST",apiEndpoint)

But as I said above nothing actually happens in the CRM

However looking at the helpfile it looks like the request send needs 2 strings(?)

MyConnection.Send("GET", "http://127.0.0.1:8080/GetData", outputFile)

But when I do it with this code, It tells me not enough parameters.

request.send("POST", apiEndpoint, HandleResponse)

So not sure if I am supposed to use the HandleResponse in here. 

Also I have a method defined, which is using a old deprecated class  Xojo.Data 






var responseJson As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(responseText)

If responseJson.HasKey(“itemId”) Then
MsgBox(“Customer created successfully!”)
Else
MsgBox("Error: " + responseJson.Value(“message”))
End If


I have a feeling this is where it lies

I’m not sure we are reading the same help file


The 3rd parameter is either a folderitem to hold the return results, or a timeout integer.
When sending asynchronously, the return will fire an event

Send method As String, URL As String, file As FolderItem, timeout As Integer = 60
method As String, URL As String, timeout As Integer = 60

you can do it synchronously, but that can tie up the UI while it waits for a response.

SendSync(method As String, URL As String, file As FolderItem, timeout As Integer)

Synchronously sends a request using an HTTP method such as GET or POST and returns the output in file. The results are saved to the supplied file. Send can be used to send both standard http and secure https requests. The timeout is in seconds.

The help also says if you are expecting a response back, then the method is more likely to be a GET than a POST.

Given that no company was created, I would expect an error response, and such a response to be
‘invalid user name/password’ or ‘company code exists’ or '404 : not found’ or similar.

(When I was doing this kind of work as ‘work’, I always used to make use of PostMan software to debug the responses in real time)

It’s worth noting that the Paw api tool is now free and that extensions are available for generating Xojo code.

No good for Catalina. <sulks>

@Jeff_Tullin

Fair enough. I am getting nothing

The response is dealt with in the method

Var responseJson As Dictionary = Xojo.Data.ParseJSON(responseText)

If responseJson.HasKey("itemId") Then
  MsgBox("Customer created successfully!")
Else
  MsgBox("Error: " + responseJson.Value("message"))
End If

I am wondering if this is the problem

Var responseJson As Dictionary = Xojo.Data.ParseJSON(responseText)

Because I think that Xojo.Data has been deprecated but I don’t know what to replace it with

Show us responseText as a mere string so we could know what we are dealing with.

Its WORKING !!! (Sort of) Thanks for everyones help. I know I have been a pain but I managed to get it working now with everyone’s help

Here is the complete code.


Var  apiUsername As String = "usernam"
Var  apiPassword As String = "Password"
Var  integrationCode As String = "Key"
Var apiEndpoint As String = "https://webservices16.autotask.net/atservicesrest/V1.0/Companies"

If TextField1.Text = "" Or TextField2.Text = "" Or TextField3.Text = "" Or TextField4.Text = "" Or TextField5.Text = "" Or TextField6.Text = "" Then
  MsgBox("Please fill in all fields.")
  Return
End If


Var  requestBody as New JSONItem
requestBody.Value("companyType") = 1
requestBody.Value("ownerResourceID") = 29682885
requestBody.Value("companyName") = TextField1.Text
requestBody.Value("Phone") = TextField2.Text
requestBody.Value("WebAddress") = TextField3.Text
requestBody.Value("City") = TextField4.Text
requestBody.Value("State") = TextField5.Text
requestBody.Value("CountryID") = TextField6.Text


Var request As New URLConnection
request.SetRequestContent(requestBody.ToString, "application/json")
request.RequestHeader("ApiIntegrationCode") = integrationCode
request.RequestHeader("UserName") = apiUsername
request.RequestHeader("Secret") = apiPassword

request.send("POST",apiEndpoint)

Then I have a method with this (which I have changed a bit)

Method Name - Handle Response (responseText as Text)

With the following in the method

Var Dict as Dictionary 
dict = ParseJSON(responseText)


if Dict.HasKey("ItemId") Then 
  MessageBox("Customer Created Successfully")
  
Else 
  
  MessageBox ("Error: " + Dict.Value("message"))
End if

Its working in so much as its writing the record via the API into the CRM but I am not getting the message to say Customer Created Successfully.

But happy its actually working at least

Xojo.Data.ParseJSON has been replaced by simply ParseJSON.

Edit: I see you found that while I was typing.

@Tim_Hare Yes I did . Thank you .

Now just to figure out why it does not tell me the customer has created correctly!!!

Ok I really cant figure out why its not telling me that the account has been created

Do I need to call the method somehow

Where are you telling anything or anyone to call HandleResponse() ?? In fact (though I may have missed it) I don’t see you doing anything vis-a-vis a reponse (such as asking for one or waiting for one).

Examine the raw json you get back from the call. There may be a key/value that you’re missing. Can you post the raw json here?

Nothing in this line receives the return data, either as text or as a file.

Maybe instead of your handler method, you need to add a handler for
ContentReceived

ContentReceived URL As String, HTTPStatus As Integer, content As String

Thanks for your help here @Jeff_Tullin

I don’t really know how to use the ContentReceived. WOul dyou mind helping me

When return data arrives, the ContentReceived event fires. If you don’t provide an event handler for that event, then nothing happens; if you do, your event handler is called as @Jeff_Tullin describes. Up to you what you do with the returned data. You could call your HandleResponse method, for instance. You might want to refer to the doc for URLConnection and look in the Events section.

BTW, assuming your code you described above as “Here is the complete code.” is in Pressed event handler for some button or other, I note that your variables request and requestBody are local to that Pressed event handler. That means that when it completes, those variables will evaporate and that could affect whether your thing will work at all. Those two you should move from being locally declared and into being properties of the window enclosing the button. You’ll still need to initialise them with new, of course.

Edit: oh, and to add the ContentReceived event handler to your request URLConnection, you’ll need to use AddHandler, so you should read up on that too.