Advise on httpClient

Post your Gambas programming questions here.
Post Reply
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Advise on httpClient

Post by AndyGable »

I am trying to get my Linux version of my card processing module working but I am getting a error.

"Invalid authorization credentials provided"

Below is my code i am currently using

Code: Select all

Dim hClient as HttpClient
Dim sBuffer as String
  
hClient = New HttpClient as "hClient"
 
With hClient 
       .URL = Global.PS_URL &"/terminals/" & global.TerminalNumber
       .Auth = 1
       .User = Base64$(global.PS_User)
       .Password = Base64$()Gl;obal.PS_Pass)
       '.Headers.Add("Software-House-ID :SD45T92")
       '.Headers.Add("Installer-Id : SD45T92")
       .Sync = False
       .Timeout = 60
       .Get
End With
the global.PS_URL and global.TerminalNumber are populated when the application starts and it creates the correct URL needed

Can someone tell me how I can send the user name and Password to the cloud server, Also as you can see the 2 headers.add are
commented out as when I try to include them I get a error (do I need to you a different method of sending that information to
the cloud server I am talking to.

Any help is most appreciated as once I get this working I should be able to work out how to read the Json data that is returned
(if anyone has any examples of this they would be very much appreciated)

Andy
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Advise on httpClient

Post by cogier »

What does the URL start with? If it is sftp you may need to shell to Curl as I can't get Gambas to handle secure sites.

If so, try something like this: -
Shell "curl -k 'sftp://MySite.com/File.json' --user 'MyUserName:MyPassword' -o " & User.Home &/ File.json Wait
This will get the File.json from the root directory of MySite.com and save it in your Home folder as File.json
AndyGable
Posts: 359
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

Re: Advise on httpClient

Post by AndyGable »

The url starts https (https://domain-example.test.connect.pay ... .cloud/pac)

In Windows i can get the terminal status via the following code

Code: Select all

Try
            Dim Request As HttpWebRequest = HttpWebRequest.Create(PS_URL & "/terminals/" & TerminalIDNumber)
            Dim credentials As String = Convert.ToBase64String(Encoding.ASCII.GetBytes(PS_USER & ":" & PS_PASS))
            txtResults.Text = vbNullString

            With Request
                .Proxy = Nothing
                .Headers(HttpRequestHeader.Authorization) = String.Format("Basic {0}", credentials)
                .Accept = "application/connect.v2+json"
                .Headers.Add("Software-House-Id: SD459T92") ' algPoS ID Number
                .Headers.Add("Installer-Id:" & InstallerID)
                .UserAgent = PS_USER
            End With

            Dim response As HttpWebResponse = Request.GetResponse()
            Dim dataStream As Stream = response.GetResponseStream
            Dim reader As New StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()

            txtResults.Text = responseFromServer


            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
        Catch ex As Exception
            addtoStatusList(ex.ToString)
            If DebugActive = "Yes" Then AddToDebugList(ex.ToString)
            If DontSendDataToPoS = 0 Then SendToPoSterminal("ProcessingError")
            FromPoSTCP.Stop()
            FromPoSTCP.Start()
        End Try
        
The above works perfectly so I need to work out how to make it work in Gambas (once i have this I can can crack on with the rest of the module)
Post Reply