B4J Question Curl with b4x

kostefar

Active Member
Licensed User
Longtime User
Dear All,

I´m using jokhttputils2 trying to authenticate a request with the following:

B4X:
    httpjob.Username = "user"
        httpjob.Password = "pass"

       
    httpjob.PostString ( "https://api.vat-search.eu/v2/bulk_search/?cutoff_score=1" , $"[
{"n": "Linkomat GmbH","a": "1150","cc": "at"},
{"n": "Google","a": ""}
]"$)
)

Based on that in the manual it states:

Example: curl -XPOST "https://api.vat-search.eu/v2/bulk_search/?cutoff_score=1" -d '[ {"n": "Linkomat GmbH","a": "1150","cc": "at"}, {"n": "Google","a": ""} ]' 2. Authentication We support Basic Authentication with the assigned user name and password setup

But I keep getting

B4X:
{"error":"Authentication required"}

I manage to logon to their web interface via chrome easily, but the API just won´t work for me. I tried having username & password both before and after the poststring.

Any idea what´s going wrong?
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Link to documentation?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
HttpJob correctly creates the basic authentication header (by request). It looks like the username and password you are providing is not correct. I verified HttpJobs basic authentication against postman-echo.com:
B4X:
'Non-UI application (console / server application)
#Region Project Attributes 
    #CommandLineArgs:
    #MergeLibraries: True 
#End Region

Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
    testpost
    StartMessageLoop
End Sub

Sub testpost
    Dim job As HttpJob
    job.Initialize("", Me)
    'job.Username = "[email protected]"
    'job.Password = "guessme!"
    job.Username = "postman"
    job.Password = "password"
    job.Download("https://postman-echo.com/basic-auth")
    'job.Download("https://api.vat-search.eu/v2/?n=Linkomat&cc=at")
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Log("Success!!!")
        Log(job.GetString)
    Else
        Log("OOOPS")
        Log(job.ErrorMessage)
    End If
    job.Release
    StopMessageLoop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
HttpJob correctly creates the basic authentication header (by request). It looks like the username and password you are providing is not correct. I verified HttpJobs basic authentication against postman-echo.com:
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
  
End Sub

Sub AppStart (Args() As String)
    testpost
    StartMessageLoop
End Sub

Sub testpost
    Dim job As HttpJob
    job.Initialize("", Me)
    'job.Username = "[email protected]"
    'job.Password = "guessme!"
    job.Username = "postman"
    job.Password = "password"
    job.Download("https://postman-echo.com/basic-auth")
    'job.Download("https://api.vat-search.eu/v2/?n=Linkomat&cc=at")
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Log("Success!!!")
        Log(job.GetString)
    Else
        Log("OOOPS")
        Log(job.ErrorMessage)
    End If
    job.Release
    StopMessageLoop
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Thanks so much! I assume that there´s no difference between whether this is NON_UI or UI, which is what I have? Silly question, only trying to find any possible reason other than the credentials not being correct before I go back to the support of the service again.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Thank you,

I ended up doing the following which worked:

httpjob.GetRequest.SetHeader ("Authorization", "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")



The base64 encoded string "username:password" encoded.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I ended up doing the following which worked:
The sad thing is, you should not have to do this. I used PTS-V2 (https://ptsv2.com) to test authentication and it just works. Even the UI version. The header looks just like it should.
B4X:
#Region Project Attributes
   #MainFormWidth: 600
   #MainFormHeight: 600
#End Region

Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
   MainForm.Show
   testpost
   
End Sub

Sub testpost
   Dim job As HttpJob
   job.Initialize("", Me)
   job.Username = "[email protected]"
   job.Password = "guessme!"
   'job.Username = "postman"
   'job.Password = "password"
   'job.Username = "test"
   'job.Password = "test"
   job.PostString("https://ptsv2.com/t/createyourowntoilet/post","test")
   'job.Download("https://postman-echo.com/basic-auth")
   'job.Download("https://api.vat-search.eu/v2/?n=Linkomat&cc=at")
   Wait For (job) JobDone(job As HttpJob)
   If job.Success Then
       Log("Success!!!")
       Log(job.GetString)
   Else
       Log("OOOPS")
       Log(job.ErrorMessage)
   End If
   job.Release
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
The sad thing is, you should not have to do this. I used PTS-V2 (https://ptsv2.com) to test authentication and it just works. Even the UI version. The header looks just like it should.
B4X:
#Region Project Attributes
   #MainFormWidth: 600
   #MainFormHeight: 600
#End Region

Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
   MainForm.Show
   testpost
  
End Sub

Sub testpost
   Dim job As HttpJob
   job.Initialize("", Me)
   job.Username = "[email protected]"
   job.Password = "guessme!"
   'job.Username = "postman"
   'job.Password = "password"
   'job.Username = "test"
   'job.Password = "test"
   job.PostString("https://ptsv2.com/t/createyourowntoilet/post","test")
   'job.Download("https://postman-echo.com/basic-auth")
   'job.Download("https://api.vat-search.eu/v2/?n=Linkomat&cc=at")
   Wait For (job) JobDone(job As HttpJob)
   If job.Success Then
       Log("Success!!!")
       Log(job.GetString)
   Else
       Log("OOOPS")
       Log(job.ErrorMessage)
   End If
   job.Release
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

Agree, it´s very strange why that other site won´t do what it should logging on in a proper manner with basic authentication but what matters is that it now works for me.

Thanks again for trying!
 
Upvote 0
Top