B4J Question HttpJob - problem with request/response

DarkoT

Active Member
Licensed User
Hello, I'm asking for help.
I created a WebApi server that responds to calls and returns appropriate results (I'm using a WEBAPI server template created by Aeric). Everything works fine and without problems, except for one thing. When my WebServices should call another WebServer and I want to initialize an HTTP Job, the system no longer returns any response to me. The function is executed correctly, without errors - to the end - but the system does not return any text or string... I can't find the error...

Example:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    HRM.Initialize

    ' Key confirmation
    If Main.API_KEY_ENABLED = True Then
        Dim apiKey As String = req.GetHeader("x-api-key")

        If apiKey <> Main.API_KEY Then
            HRM.ResponseCode = 401
            HRM.ResponseError = "Wrong api key"
            HRM.ResponseString = "API key is not valid"
            HRM.ResponseMessage = "API key is not valid"
            HRM.ContentType = ""
            Dim lstNo As List
            lstNo.Initialize
            HRM.ResponseData = lstNo
            Utility.ReturnHttpResponse(HRM, Response)
            Return
        End If

    End If

    ' checking allowed method
    If req.Method <> "POST" Then
        HRM.ResponseCode = 405
        HRM.ResponseError = "Method Not Allowed"
        Utility.ReturnHttpResponse(HRM, Response)
        Return
    End If

    Dim ApiRequest As String = Request.RequestURI
    ApiRequest = ApiRequest.SubString2(ApiRequest.LastIndexOf("/") + 1 , ApiRequest.Length)
    ApiRequest = ApiRequest.ToLowerCase

    Dim lstOptEcho As List
    lstOptEcho.Initialize
    
    ' subhandlers
    Select ApiRequest.ToLowerCase
        Case "hallo"
            EchoRequest
        
        Case "test"
            TestRequest

        Case "sendorder"
            Try 
            ...

SubRutine for simple echo...

Sub:
Private Sub TestRequest()
    
    ' HERE IS A PROBLEM; IF I DEFINE HTTPJOB, system lose response
    Dim j As HttpJob
    j.Initialize("j", Me)
    j.Download("https://www.google.com")
    Wait For (j) JobDone (j As HttpJob)
    j.Release

    Response.ContentType = "text/plain"
    Response.Status = 200
    Response.Write("SUCCESS")
    Log("Response sent successfully")
    
    
End Sub

Can somebody help me to figure out what is a problem?

Best regards, DaT
 
Solution
Take a look at this:
Though that was needing only the following...

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
   Download(resp)
   StartMessageLoop '<---
End Sub

Sub Download (resp As ServletResponse)
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("https://www.example.com")
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
     resp.Write(j.GetString)
   End If
   j.Release
   StopMessageLoop '<----
End Sub

That because webpages working asychronous...

What extra gives if use all the things in url ?
I didn't understand welll

aeric

Expert
Licensed User
Longtime User
my WebServices should call another WebServer and I want to initialize an HTTP Job
Take a look at this:
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
Take a look at this:
Though that was needing only the following...

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
   Download(resp)
   StartMessageLoop '<---
End Sub

Sub Download (resp As ServletResponse)
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("https://www.example.com")
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
     resp.Write(j.GetString)
   End If
   j.Release
   StopMessageLoop '<----
End Sub

That because webpages working asychronous...

What extra gives if use all the things in url ?
I didn't understand welll
 
Upvote 0
Solution

DarkoT

Active Member
Licensed User
Though that was needing only the following...

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
   Download(resp)
   StartMessageLoop '<---
End Sub

Sub Download (resp As ServletResponse)
   Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("https://www.example.com")
   Wait For (j) JobDone(j As HttpJob)
   If j.Success Then
     resp.Write(j.GetString)
   End If
   j.Release
   StopMessageLoop '<----
End Sub

That because webpages working asychronous...

What extra gives if use all the things in url ?
I didn't understand welll
Magma, Tnx...

This is solution!!!!

p.s. You desarve a biiiiig cafffeeeeee... ;)

Br, DaT
 
Upvote 0

DarkoT

Active Member
Licensed User
Take a look at this:
Yes! this was basicaly solution - with Magmas explanation... Tnx guys... ;)
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
But as I said - yes my apps are working too...

but i am sure there is a reason that all these need... @aeric tell us, why? - because i ve read Erel's post and i didn;t understand where jbuilder... and other things (SERVER at conditional) need...

do you know ? - is there a concurrency problem ?
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
do you know ? - is there a concurrency problem ?
Honestly, I haven't have a chance to implement this.
My understanding is it may related to multi-thread.
One thing to find out is try to run in release mode. Because if you run in debug, it is using a single thread.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I was quickly replying the post.
I think the following code is more relevant as explained by Magma.
 
Upvote 0
Top