Android Question [Solved]HttpJob

Ale_resource

Member
Licensed User
Hi I should download a json from a web service via an api call ,This is the code I used :
B4X:
    Dim JobDoc As HttpJob
    JobDoc.Initialize("JobDoc",Me)
    JobDoc.Download("https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100009")
    JobDoc.GetRequest.SetHeader("APIKey","2b6dedd8efc2668b56b2d070424a436f0027134c293199724ee80.........")
    JobDoc.GetRequest.SetHeader("APISecret","47c55ace70212b27d79db9e5824d09541ea120de3e88bf8993c4152c62239b0df9382242e9570dd00e6cfcf3980ba74a95630305cf00661fe89d8......."")
    JobDoc.GetRequest.SetHeader("CodiceCliente","0185469.....")
    Wait For (JobDoc) JobDone(JobDoc As HttpJob)
    If JobDoc.Success Then
        Log(JobDoc.GetString)
    Else
        Log(JobDoc.ErrorMessage)
    End If

and this is the documentation :



but i can't download anything, i always get this error



I tried with Postman to manage the api and it works fine.
What am I doing wrong ?
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
when you say an empty response do you get the same 401 error. You can see this in postman at the right hand side of the results window.

 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
As another test can you use the Code snippet option to display the working version as Java-okhttp.






Then cut and paste that into the b4a code.
 
Upvote 0

Ale_resource

Member
Licensed User
Sorry , I can't help further without the the actual code. Is it possible to create some temporary API keys to use?
here is the data i am using:
Url = https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100012
ApiKey = ce8ac27ca1c73f5afd1d2f66976aa6be88335fe6e282a921acc963d575163b6e
APISecret = e290984a3d0d8ae286306dc152b5493c70b637ffcc48f2049ba90cab73f3e26c337fa2fc97379ce9b52586b98ae92c3934bf261731d095a949a47fbb2853f58b
CodiceCliente =01854690433
Method = Get
 
Upvote 0

Ale_resource

Member
Licensed User

OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100012")
.method("GET", null)
.addHeader("APIKey", "ce8ac27ca1c73f5afd1d2f66976aa6be88335fe6e282a921acc963d575163b6e")
.addHeader("APISecret", "e290984a3d0d8ae286306dc152b5493c70b637ffcc48f2049ba90cab73f3e26c337fa2fc97379ce9b52586b98ae92c3934bf261731d095a949a47fbb2853f58b")
.addHeader("CodiceCliente", "01854690433")
.build();
Response response = client.newCall(request).execute();
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Ahhh...

In my haste, I mistakenly used HTTP and HTTPUTILS libraries in the program.

This works.

HOWEVER, when you change them to OKHTTP and OKHTTPUTILS2 libraries it does NOT work.

Is this a bug in the library?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
it seems as if using the download only 1 header is taken and the others are excluded
No. Using ptsv2.com, I created a "toilet" and all headers are transferred. Looking at what postman sends and what okhttp2utils2/okhttp sends, I don't really see any breaking issues. This one is a head scratcher.

okhttp2utils2/okhttp headers not working (I've modified the header output to take out my IP address)
Postman headers (modified to be more like above) working:
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Code used for the above
B4X:
    'Dim url As String = "https://ptsv2.com/t/**************/post"
    Dim url As String = "https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100012"
    Dim apiKey As String = "ce8ac27ca1c73f5afd1d2f66976aa6be88335fe6e282a921acc963d575163b6e"
    Dim apiSecret As String = "e290984a3d0d8ae286306dc152b5493c70b637ffcc48f2049ba90cab73f3e26c337fa2fc97379ce9b52586b98ae92c3934bf261731d095a949a47fbb2853f58b"
    Dim codiceCliente As String = "01854690433"
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(url)
    j.GetRequest.SetHeader("APIKey",apiKey)
    j.GetRequest.SetHeader("APISecret",apiSecret)
    j.GetRequest.SetHeader("CodiceCliente",codiceCliente)
    j.GetRequest.SetHeader("Content-Length", 0)
    j.GetRequest.SetHeader("Cache-Control", "no-cache")
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then
        xui.MsgboxAsync($"Success:
--- GetString
${j.GetString}"$, "B4X")
    Else
        xui.MsgboxAsync($"Failure (${j.Response.StatusCode}):
--- ErrorMessage
${j.ErrorMessage}"$, "B4X")
    End If
    j.Release
 
Upvote 0

Ale_resource

Member
Licensed User
in my opinion not all the lines set in the header are sent, in fact if I call another api on which there is no need for header it works. moreover and from postman I remove one of the 3 headers it returns me the error 401
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
in my opinion not all the lines set in the header are sent
Did you look at the output of the ptsv2.com site? The headers ARE sent. All of them. And the removed headers are not. Try it for yourself. Create a "toilet" at the site and run your request against the provided link (should be in the form of https://ptsv2.com/t/**************/post, with the *'s being your toilet id).
 
Upvote 0

Ale_resource

Member
Licensed User
at this point then i have no idea what it could be, i tried to use the library HttpUtils2
and it works while with OkHttpUtils2 doesn't work with
 
Upvote 0

Ale_resource

Member
Licensed User
I also tried with the code below but I have the exact same problem.
B4X:
Sub Button1_Click
    Dim url As String = "https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100012"
    Dim apiKey As String = "ce8ac27ca1c73f5afd1d2f66976aa6be88335fe6e282a921acc963d575163b6e"
    Dim apiSecret As String = "e290984a3d0d8ae286306dc152b5493c70b637ffcc48f2049ba90cab73f3e26c337fa2fc97379ce9b52586b98ae92c3934bf261731d095a949a47fbb2853f58b"
    Dim codiceCliente As String = "01854690433"
    Dim hc As OkHttpClient
    hc.InitializeAcceptAll("hc")
    Dim req As OkHttpRequest
    req.InitializeGet(url)
    req.SetHeader("APIKey",apiKey)
    req.SetHeader("APISecret",apiSecret)
    req.SetHeader("CodiceCliente",codiceCliente)
    hc.Execute(req,1000)

End Sub


Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   out.InitializeToBytesArray(0)
   Response.GetAsynchronously("res", out, False, TaskId)
End Sub

Sub res_StreamFinish (Success As Boolean, TaskId As Int)
   Log($" ${Success}"$)
   If Success Then Log(BytesToString(out.ToBytesArray, 0, out.ToBytesArray.Length, "utf8"))
End Sub


Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log($"Error: ${Response.ErrorResponse}, ${Reason}, ${StatusCode}"$)
   If Response <> Null Then Response.Release
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…