iOS Question iHttpUtils Response string is empty on error

Status
Not open for further replies.

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Using iHttpUtils, when the response status code is not 200, the response string is empty.

The API I am using returns the error information in a JSON string.

With B4A, I can get this string on an error response, with B4I it is empty.

The status code is correct through.

This is using the latest iHTTPUtils, 2.71.
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Here is the code
B4X:
Sub LoginUser(aUser As UserInfo)
    Private job As HttpJob
    Private jgen As JSONGenerator
    Private s As String
    Private mp As Map
        
    
    mp.Initialize
    mp.Put("emailaddress",aUser.Email)
    mp.Put("password",aUser.Password)
    
    jgen.Initialize(mp)
    
    s = jgen.ToString
    
    job.Initialize("a",Me)
    job.PostString(Starter.gURLLoginUser,s)
    job.GetRequest.SetHeader("Accept","application/json")
    job.GetRequest.SetHeader("Authorization",Starter.AuthKey)
    job.GetRequest.SetContentType("application/json")
    Wait For (job) JobDone(job As HttpJob)
    If job.Success Then
        Private t As String = job.GetString
        ParseLoginReturn(t,aUser)
    Else
#if B4i
        Private hr As HttpResponse = job.Response
        private t  as string =hr.GetString
        Log("Headers" & hr.GetHeaders)
        Log("Status Code " & hr.StatusCode)
        Log("return t = " & t)
#else
    private t as string = job.getstring
#end if
        If (t.Length > 1 And t.SubString2(0,1) = "{" And t.EndsWith("}")) Then
            ParseErrorReturn(t)
        Else
            Msgbox("Problem with login."&CRLF & job.ErrorMessage,Starter.gstrApplicationTitle)
        End If
    End If
    
    job.Release
        
End Sub

This is what is returned in B4A, in B4i The reponse string is empty and the error is unknown error.

B4X:
{
    "code": "no_authorization",
    "message": "You do not have access to use this API",
    "data": {
    "status": 401
    }
}
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Huh? I thought Job.GetString is empty when Job.Success = False? At that point, Job.ErrorMessage contains the returned result.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
@Erel says in the post you linked you should use job.GetString for B4i, but the code you post above does not do that. It uses job.GetString for all platforms but B4i. Should your code not be
B4X:
#if B4i
        private t  as string = job.GetString
#else
        private t as string = job.ErrorMessage
#end if
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Sorry, I initially mis-read you message.

I have just tried the code you suggested above.

t is now an empty string. So the information is getting lost somewhere.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Logging the object just returns "<B4IHttpResponse: 0x1c50360c0>"
However, logging each of the internal fields in turn - the header gives, amongst other things:

"Content-Length" = 100;
"Content-Type" = "application/json; charset=UTF-8";

That is the correct length for the unformatted JSON string.

GetString and GetsString2 are both blank.

The InputStream object is not initialized.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

JackKirk

Well-Known Member
Licensed User
Longtime User
I've just spent the day banging my head on this - is there any chance of a fix or workaround?
 
Upvote 0
Status
Not open for further replies.
Top