B4J Question Curl with b4x

Discussion in 'B4J Questions' started by kostefar, Jul 30, 2019.

  1. kostefar

    kostefar Active Member Licensed User

    Dear All,

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

    Code:
    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:

    But I keep getting

    Code:
    {"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: Jul 30, 2019
  2. OliverA

    OliverA Well-Known Member Licensed User

    kostefar likes this.
  3. kostefar

    kostefar Active Member Licensed User

    Sorry, my bad. Corrected it now. Result is like before though.
     
  4. OliverA

    OliverA Well-Known Member Licensed User

    Link to documentation?
     
    kostefar likes this.
  5. kostefar

    kostefar Active Member Licensed User

    https://vat-search.eu/src/pdf/docs2.pdf

    Which also contains a GET method that´s simpler, but gives the same result.

    I should add that I also tried with
    Code:
    user:password@URL
    authentication, but with no luck either.
     
  6. OliverA

    OliverA Well-Known Member Licensed 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:
    Code:
    '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 = "johndoe@somewhere.net"
        '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 StringAs Boolean
        
    Return True
    End Sub
     
    kostefar, Erel and DonManfred like this.
  7. kostefar

    kostefar Active Member Licensed User

    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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    True. Just remove StartMessageLoop / StopMessageLoop calls.
     
    kostefar likes this.
  9. kostefar

    kostefar Active Member Licensed User

    Thank you,

    I ended up doing the following which worked:

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



    The base64 encoded string "username:password" encoded.
     
    OliverA and DonManfred like this.
  10. OliverA

    OliverA Well-Known Member Licensed 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.
    Code:
    #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 = 
    "johndoe@somewhere.net"
       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 StringAs Boolean
       
    Return True
    End Sub
     
    kostefar likes this.
  11. kostefar

    kostefar Active Member Licensed User

    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!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice