Page 1 of 2
Help Converting From VB.net to Gambas
Posted: Monday 28th June 2021 10:28pm
by AndyGable
Code: Select all
Dim sBuffer As String
Dim Request As HttpClient = HttpClient.Create(Global.PS_URL & "/terminals/" & Global.TerminalIDNumber)
Dim credentials As String = Base64$(Global.PS_USER & ":" & Global.PS_PASS)
With Request
.Proxy = Null
.Headers = Format("Basic {0}", credentials)
.UserAgent = Global.PS_USER
End With
If Request.Status < 0 Then
Print "ERROR"
Else
' Success - read the data
If Lof(Request) Then sBuffer = Read #Request, Lof(Request)
Print sBuffer
End If
'If responseFromServer = "0" Then
' MsgBox("Retreal of Status Failed")
'Else
' Dim json As String = responseFromServer
' Dim ser As JObject = JObject.Parse(json)
' Dim data As List( Of JToken) = ser.Children().ToList
' For Each item As JProperty In data
' item.CreateReader()
' Select Case item.Name
' Case "status"
' Select Case item.Value
' Case "AVAILABLE"
' GetStatus = 1
' If DontSendDataToPoS = 0 Then SendToPoSterminal("TerminalOnLine|")
' Me.ListBox1.Items.Clear
' addtoStatusList("Terminal Ready")
'
' Case "BUSY"
' If DontSendDataToPoS = 0 Then SendToPoSterminal("TerminalBusy|")
' addtoStatusList("Terminal busy please wait 10 seconds and try again")
'
' Case "Offline", "OFFLINE", "offline"
' If DontSendDataToPoS = 0 Then SendToPoSterminal("offline|")
' addtoStatusList("Terminal OFFLINE NO Card processing Possible - Please check with Payment Sense")
' End Select
' End Select
' Next
' End If
Hi Everyone,
I have now spent 2 days trying to convert this simple function to Gambas (once I can get this done I should be able to get the other more complicated function converted)
but no matter what I tried to do or google i get nothing zip sod all (and I get errors on the Dim Request As HttpClient line)
can someone more smarter then me help me convert this so it works?
I will need to get the JSON support working as well as that is the format the data is returned from the Web request.
Thank-you in advice for any help
Andy
Re: Help Converting From VB.net to Gambas
Posted: Monday 28th June 2021 11:26pm
by BruceSteers
Try something like ...
Dim Request As New HttpClient
Request.URL = Global.PS_URL & "/terminals/" & Global.TerminalIDNumber
Re: Help Converting From VB.net to Gambas
Posted: Tuesday 29th June 2021 6:09pm
by AndyGable
BruceSteers wrote: ↑Monday 28th June 2021 11:26pm
Try something like ...
Dim Request As New HttpClient
Request.URL = Global.PS_URL & "/terminals/" & Global.TerminalIDNumber
I tried that and now I am getting Null object (PSConnect:14) on the Request.URL line
but the URL is being made correclty (https://**.*.*.paymentsense.cloud/*/terminals/22165264) (some of the address have been removed for security)
Am I doing this completely wrong?
Re: Help Converting From VB.net to Gambas
Posted: Wednesday 30th June 2021 2:00pm
by cogier
Have you added the gb.net.curl Component? 'HttpClient' won't work without it. You will also need gb.web for JSON decoding.
Re: Help Converting From VB.net to Gambas
Posted: Wednesday 30th June 2021 7:40pm
by BruceSteers
AndyGable wrote: ↑Tuesday 29th June 2021 6:09pm
BruceSteers wrote: ↑Monday 28th June 2021 11:26pm
Try something like ...
Dim Request As New HttpClient
Request.URL = Global.PS_URL & "/terminals/" & Global.TerminalIDNumber
I tried that and now I am getting Null object (PSConnect:14) on the Request.URL line
but the URL is being made correclty (https://**.*.*.paymentsense.cloud/*/terminals/22165264) (some of the address have been removed for security)
Am I doing this completely wrong?
Possibly.
if you look at
http://gambaswiki.org/wiki/comp/gb.net.curl/httpclient
making a new httpclient object and using URL is correct.
your URL must have the null object in it
Try ...
Debug Global.PS_URL
Debug Global.TerminalIDNumber
PS. use &/
not just & and then putting / in the strings as using &/ does / automatically if needed or not
Re: Help Converting From VB.net to Gambas
Posted: Wednesday 30th June 2021 9:15pm
by BruceSteers
If Global.TerminalIDNumber is not a string you must use Str(Global.TerminalIDNumber)
Re: Help Converting From VB.net to Gambas
Posted: Friday 2nd July 2021 11:31pm
by AndyGable
Hi Everyone
I have tired what you all recommend and I can say for sure the URL is being created correctly beacuse if I click it I get the following error message on the website
Code: Select all
{"messages":{"error":["Missing authorization credentials."]}}
The url it is createing is
Code: Select all
https://test.connect.paymentsense.cloud/pac/terminals/123456789
(the Terminal Number is a fake one but this is the URL it makes)
This is my code so far
Code: Select all
Dim ReqURL As String = Global.PS_URL & "/terminals/" & Str(Global.TerminalIDNumber)
Print ReqURL
Dim sBuffer As String
Dim Request As HttpClient
Dim credentials As String = Base64$(Global.PS_USER & ":" & Global.PS_PASS)
Request.URL = "" & ReqURL & ""
Request.Headers = Format("Basic {0}", credentials)
Request.UserAgent = Global.PS_USER
Request.Get
If Request.Status < 0 Then
Print "ERROR"
Else
' Success - read the data
If Lof(Request) Then sBuffer = Read #Request, Lof(Request)
Print sBuffer
End If
and I have the following componets
- gb.form
- gb.gui.qt
- gb.image
- gb.net
- gb.net.curl
- gb.settings
- gb.utl.web
- gb.web
am I missing any?
Re: Help Converting From VB.net to Gambas
Posted: Saturday 3rd July 2021 12:08am
by BruceSteers
no your error is in passing the credentials.
have you tried setting Request.Password and Request.User ? (maybe before setting URL?)
also there is simply using HttpClient.Download ( URL As String , Headers As String[] ) As String
also .Headers is String[] should be in angle brackets even if only one item
So it should be Request.Headers = [one_header, another_header]
Also your use of Format() does not make sense looking at
http://gambaswiki.org/wiki/lang/format
Re: Help Converting From VB.net to Gambas
Posted: Saturday 3rd July 2021 8:55pm
by AndyGable
Hi Everyone,
I have made some progress now but i am getting Error code 6 (Unable to Resolve Host address) but when I click on the url and it
opens in Chrome I can see the webpage
I have even tried to change it to Google just to test it and I am still getting the same error message.
Any ideas what I need to do
Code: Select all
Dim sBuffer As String
Dim Request As HttpClient
Request = New HttpClient As "Request" <<---- MISSED THIS LINE OFF the code (once I saw it on the example I added it)
With Request
.User = Base64$(Global.PS_USER)
.Password = Base64$(Global.PS_PASS)
.URL = "" & Global.PS_URL & "/terminals/" & Str(Global.TerminalIDNumber) & ""
.Get
End With
Print Request.Status
If Request.Status > 0 Then
HttpRequestErrorCode(Request.Status)
Else
' Success - read the data
If Lof(Request) Then sBuffer = Read #Request, Lof(Request)
Print sBuffer
End If
Re: Help Converting From VB.net to Gambas
Posted: Monday 5th July 2021 2:02pm
by AndyGable
Hi Everyone,
I have spoken to my Web Service API supplier and they confirm the Call is reaching their end but I am not sending the correct data that is why I am getting the Error code 6 (401 on thier end)
What I need to send to them is the following
Content-Type header = application/json
Accept header (API version) = application/connect.v2+json
Authorization header
Software-House-Id and Installer-Id headers
Host address -- all ready working
any idea what options I need to use on the httpclient so the data can be sent?
So far I have the following
Code: Select all
With Request
.Auth = 1 ' BASIC authorization
.User = Base64$(Global.PS_USER)
.Password = Base64$(Global.PS_PASS)
.URL = Global.PS_URL & "/terminals/" & Str(Global.TerminalIDNumber)
.Get
End With
Any advice is welcomed as once I have this single connection working i can port the rest of my application over to Linux