iOS Question iHttpUtils Response string is empty on error

Discussion in 'iOS Questions' started by Andrew (Digitwell), Sep 16, 2018.

  1. Andrew (Digitwell)

    Andrew (Digitwell) Active Member Licensed 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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Not 2xx.

    Can you post the relevant code?
     
  3. Andrew (Digitwell)

    Andrew (Digitwell) Active Member Licensed User

    Here is the code
    Code:
    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.

    Code:
    {
        
    "code""no_authorization",
        
    "message""You do not have access to use this API",
        
    "data": {
        
    "status"401
        
    }
    }
     
    Last edited: Sep 17, 2018
  4. OliverA

    OliverA Well-Known Member Licensed User

    Huh? I thought Job.GetString is empty when Job.Success = False? At that point, Job.ErrorMessage contains the returned result.
     
  5. Andrew (Digitwell)

    Andrew (Digitwell) Active Member Licensed User

    Yes, the documentation does say this.

    Job.ErrorMessage just contains "UnknownError"

    I know that the error should actually be a JSON string with
    Code:
    {
        
    "code""no_authorization",
        
    "message""You do not have access to use this API",
        
    "data": {
        
    "status"401
        
    }
    }
    in it. This is what appears in B4A.
    see also, https://www.b4x.com/android/forum/threads/http-response-different-in-b4i.97143/
     
  6. OliverA

    OliverA Well-Known Member Licensed 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
    Code:
    #if B4i
            private t  as string = job.GetString
    #else
            private t as string = job.ErrorMessage
    #end if
     
  7. Andrew (Digitwell)

    Andrew (Digitwell) Active Member Licensed 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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    It is possible that the response body is lost when the status code is not 2xx. Put a breakpoint and examine 'hr'. See if you can find anything in its internal data.
     
  9. Andrew (Digitwell)

    Andrew (Digitwell) Active Member Licensed 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.
     
  10. OliverA

    OliverA Well-Known Member Licensed User

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