Android Question [ OkHttpUtils2 ] Bug on HttpJob Header ?

Waldemar Lima

Well-Known Member
Licensed User
hello all !

I'm trying to make a request to an API using httpjob and I'm having an "unknown" error, see the clues below :




B4X:
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("https://pay.api","")  'the request parameter is empty as it has no importance in this case as the errors are about the headers ...
    ' ######
    ' if I comment out the 2 lines below that are setting headers, I get a "coherent" result
    ' result : ResponseError. Reason: Forbidden, Response: {"HasError":true,"ErrorCode":"403","Error":"It was not possible to authenticate with the informed parameters."}
    ' if I uncomment the headers, I get an "unknown" error from the API
    ' result : ResponseError. Reason: Bad Request, Response: {"HasError":true,"ErrorCode":"999","Error":"An uncataloged error has occurred. ","RequestId":"d2xx-xxx-xxxx-xxxxx-xxxxx"}
    ' ######
'    j.GetRequest.SetHeader("X-API-KEY", "XXXXXXXXXXXXXXXXXXXXXXXXXXX")
'    j.GetRequest.SetHeader("content-type", "application/json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release

with the same information I put in postman , am I doing something wrong ?
 

Waldemar Lima

Well-Known Member
Licensed User
I talked to the support people, and they say that my request is not arriving in "JSON", and they sent this log:

B4X:
HTTP/1.1 200
Date: Wed, 16 Jun 2021 14:38:39 GMT

my code :

B4X:
'Handler class
Sub Class_Globals
    
    Private Json As String
    
End Sub

Public Sub Initialize
    
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)

    Log("Initializing Request")

    Json = resp.Write(File.ReadString(File.DirAssets,"request.json"))
    
    Dim data() As Byte
    data = Json.GetBytes("UTF8")
    
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostBytes("https://api.pagseguro.com.br/v2/Payment",data)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("X-API-KEY", "XXXXXXXXXXXXXXXXXXXXXXXX")
    Wait For (j) JobDone(j As HttpJob)

    If j.Success Then
        Log("response : "&j.GetString)
    End If
    j.Release
    
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I talked to the support people, and they say that my request is not arriving in "JSON", and they sent this log:

B4X:
HTTP/1.1 200
Date: Wed, 16 Jun 2021 14:38:39 GMT

my code :

B4X:
'Handler class
Sub Class_Globals
   
    Private Json As String
   
End Sub

Public Sub Initialize
   
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)

    Log("Initializing Request")

    Json = resp.Write(File.ReadString(File.DirAssets,"request.json"))
   
    Dim data() As Byte
    data = Json.GetBytes("UTF8")
   
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostBytes("https://api.pagseguro.com.br/v2/Payment",data)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("X-API-KEY", "XXXXXXXXXXXXXXXXXXXXXXXX")
    Wait For (j) JobDone(j As HttpJob)

    If j.Success Then
        Log("response : "&j.GetString)
    End If
    j.Release
   
End Sub
1. Why you use resp.Write ?
I think you can read the json file to json variable by using File.ReadString
2. Why you change job.PostString to job.PostBytes ?
 
Upvote 0

Waldemar Lima

Well-Known Member
Licensed User
fixed, ended up going unnoticed by me on this line >
B4X:
  Json = resp.Write(File.ReadString(File.DirAssets,"request.json"))

' I removed "resp.Write()" and fixed !!!
' it was lack of attention on my part


working code :

B4X:
'Handler class
Sub Class_Globals
   
    Private Json As String
   
End Sub

Public Sub Initialize
   
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)

    Json = File.ReadString(File.DirAssets,"request.json")

   
    Download(resp)
    StartMessageLoop '<---

End Sub

Sub Download (resp As ServletResponse)

    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("https://api.pagseguro.com.br/v2/Payment",Json)
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("X-API-KEY", "XXXXXXXXXXXXXXXXXXXXXXXXX")
    Wait For (j) JobDone(j As HttpJob)
   
    If j.Success Then
        Log("resposta : "&j.GetString)
    End If

    j.Release

   StopMessageLoop '<----
End Sub

thank you all for your attention :D
 
Upvote 0
Top