B4J Question [SOLVED] jHttpUtils2: Trapping 204 No Content

Mashiane

Expert
Licensed User
Hi there

I'm using a rest api, apparently it returns a 204 "No Content" message however my wait for statement never gets to process this.

Step 1: Get Access Token and Save it (This works perfectly!!!)

B4X:
'get the access token
Sub GetAccessToken() As ResumableSub
    Log("GetAccessToken")
    'set access token to blank
    access_token = ""
    Dim job As HttpJob
    'initialize a http request
    job.Initialize("",Me)
    'pass the url to get the token
    job.PostString("<endpoint>",$"grant_type=password&username=${userName}&password=${passWord}"$)
    'set the content type
    job.GetRequest.SetContentType("application/x-www-form-urlencoded")
    'wait for the web service to complete
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        'translate to a map
        Dim strResponse As String = job.GetString
        job.release
        Dim access As Map = Json2Map(strResponse)
        'assign variables
        access_token = access.GetDefault("access_token","")
        'save the token to a file
        File.WriteString(File.DirApp,"access_token.txt",access_token)
        Return True
    Else
        If File.Exists(File.DirApp,accessTokenFile) Then File.Delete(File.DirApp,accessTokenFile)
        LogError($"A '${job.ErrorMessage}' has been experienced reading GetAccessToken!"$)
        job.Release
        Return False
    End If
End Sub

Step 2: Process some rest api calls (HERE IS THE PROBLEM)

B4X:
'Get Results 
Sub GetResults(ID As Int) As ResumableSub
    Log("GetResults")
    'read the access token from file
    ReadAccessToken
    If access_token = "" Then Return False
    'define command to read data
    Dim strCommand As String = $"<EndPoint>=${ID}"$
    'create an http request
    Dim job As HttpJob
    job.Initialize("",Me)
    job.Download(strCommand)
    'pass the access token
    job.GetRequest.SetHeader("Authorization", "bearer " & access_token)
    'wait until the job is finished
    Wait For (job) JobDone(job As HttpJob)
    <<<GetsStuckHere>>>
If job.Success Then
        'get the response
        Dim strResponse As String = job.GetString
        job.release
        log(strResponse)
        Return True
    Else
        LogError($"A '${job.ErrorMessage}' has been experienced reading the GetResults!"$)
        job.release
        Return False
    End If
End Sub

In step 2, the endpoint is valid however returns a 204 error "no content", thing is inside b4j this is never returned at all. Any ideas on how to solve this?
 

keirS

Well-Known Member
Licensed User
Changed to jOkHttpUtils2 as it has the Head object, thing is how do I call this...

B4X:
job.GetRequest.SetHeader("Authorization", "bearer " & access_token)

Thanks

Using OAuth2 it should be:

B4X:
job.GetRequest.SetHeader("Authorization", "Bearer " & access_token)

Bearer with a capital B!
 
Upvote 0

keirS

Well-Known Member
Licensed User
Noted with thanks, the issue I have is the .SetHeader method, it does not exist with jOkHttpUtils2. I need help with that.
B4X:
job.GetRequest().SetHeader("Authorization", "Bearer " & access_token)

HttpJob has a SetHeader
 
Upvote 0

keirS

Well-Known Member
Licensed User
@keirS, with B4J, still same issue, whilst the .GetRequest object is available, the chained methods don't exist with jOkHttpUtils2, this worked well with jHttpUtils2 but then that does not not have the job.Head object.

B4X:
job.GetRequest.SetContentType("application/x-www-form-urlencoded")

Are you using HTTPUtils and OKHttpUtils in the same project? Because in my experience this doesn't work and has very bad results.
 
Upvote 0

keirS

Well-Known Member
Licensed User
On a clean project:

setheader.png
 
Upvote 0
Top