B4J Question Receiving more than one response for a request

coldflame

Member
I am building a REST backend server. When I send a response using resp.write() especially during validation, the sever sends the response for the request but the request is still further processed. Hence i received more than one response for a single request. Please how can i fix it?
 

DonManfred

Expert
Licensed User
Longtime User
Where is the relevant servercode?
 
Upvote 0

coldflame

Member
Handler Code:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    If req.Method <> "POST" Then
        If req.Method <> "POST" Then
            Utility.MapToResponse(Utility.GenerateErrorMap("Only Post Operation is allowed","You cannot perform any other operation apart from post operation"),404,resp)
        End If
    End If
    
    Dim ReqMap As Map = Utility.RequestToMap(req.InputStream)
    
    Dim EmailAddress As String
    Dim Password As String
    Dim Connection As SQL = Main.ConnectionPool.GetConnection()
    Dim strQuery As String
    Dim Result As ResultSet
    Dim Cryptify As BCrypt
    
    Cryptify.Initialize("")
    EmailAddress = ReqMap.Get("email_address")
    Password = ReqMap.Get("password")
    
    'Check if any of the user or the password is empty
    
    If EmailAddress.Trim = "" Then
        Utility.MapToResponse(Utility.GenerateErrorMap("Email Address is required","You have to supply an email address. send a json as request body email_address"),400,resp)
    Else if Utility.ValidateEmail(EmailAddress.Trim) = False Then
        Utility.MapToResponse(Utility.GenerateErrorMap("Email Address format is invalid","You have to submit a valid email address with @ and ."),400,resp)
    Else if Password.Trim = "" Then
        Utility.MapToResponse(Utility.GenerateErrorMap("Password is required","You have to supply Password. send a json request body of password"),400,resp)
    Else if Password.Trim.Length < 8 Then
        Utility.MapToResponse(Utility.GenerateErrorMap("Incorrect Login Credentials","The Login Credentials you have submit in incorrect"),400,resp)
    End If
    
    'After here start a database connection
    strQuery = "SELECT * FROM authentications WHERE email_address = ?"
    Result = Connection.ExecQuery2(strQuery,Array(EmailAddress))
    
    If Result.NextRow = False Then
        Utility.MapToResponse(Utility.GenerateErrorMap("Incorrect Login Credentials","The Login Credentials you have submit in incorrect"),400,resp)
    End If
    
    'Get the Hashed Password and the compare it with the inserted password
    If Cryptify.checkpw(Password,Result.GetString("password")) = False Then
        Utility.MapToResponse(Utility.GenerateErrorMap("Incorrect Login Credentials","The Login Credentials you have submit in incorrect"),400,resp)
    End If
    
    Utility.MapToResponse(Utility.GenerateSuccessMap("Login Was Successful","You have logged in successfully",CreateMap()),200,resp)
    Connection.Close
End Sub
 
Upvote 0

coldflame

Member
Code that generate the response:
Sub MapToResponse(map As Map, status As Int,resp As ServletResponse)
    Dim jgen As JSONGenerator
    jgen.Initialize(map)
    resp.ContentType = "application/json"
    resp.Status = status
    resp.Write(jgen.ToString)
End Sub
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
You should only call resp.write() once the response is fully constructed. If you have multiple conidtions in your handler you should add a return statement after resp.write().

This is a REST handler that just returns the status of the server as a JSON string.

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim Response As String   
    Response = $"{"status":"${Main.status}","status_sub":"${Main.status_sub}","status_download":"${Main.status_download}"}"$
        
    resp.ContentType = "application/json"
    resp.Write(Response)
End Sub
 
Upvote 0

coldflame

Member
You should only call resp.write() once the response is fully constructed. If you have multiple conidtions in your handler you should add a return statement after resp.write().

This is a REST handler that just returns the status of the server as a JSON string.

B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim Response As String  
    Response = $"{"status":"${Main.status}","status_sub":"${Main.status_sub}","status_download":"${Main.status_download}"}"$
       
    resp.ContentType = "application/json"
    resp.Write(Response)
End Sub
Thanks this works fines. I added a return after every resp.write()
 
Upvote 0
Top