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 :

1620750945784.png


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

1620751535562.png


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

Star-Dust

Expert
Licensed User
Longtime User
Do you use HttpUtils or okHttpUtils?
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Set headers exactly as the ones used with postman. For example you may need to set the header of user-agent.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Try
B4X:
Dim JobDoc As HttpJob
JobDoc.Initialize("",Me) ‘Just make it “”, not “JobDoc”
JobDoc.Download("https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100009")
‘using a browser (safari on iPhone, the above without any extra headers just
‘Returns a blank page
 Wait For (JobDoc) JobDone(JobDoc As HttpJob)
 If JobDoc.Success Then
        Log(“good”)
        Log(JobDoc.GetString)
 Else
        Log(“bad”)
        Log(JobDoc.ErrorMessage)
 End If
JobDoc.Release ‘ don’t forget this
Note: you may need to replace the quotes (both double and single), since I’m typing this on an iPhone
This should give us a baseline to work with. First get this to work, then you can worry about headers
 
Upvote 0

Ale_resource

Member
Licensed User
Can you post the documentation part about setting the headers?
And also, are you using cleartext in manifest
Finally post the full error as text for us to see where the problem is actually coming from
1620802281735.png

i tried both adding cleartext in manifest and without .
the only error I see is this, it doesn't print anything unfortunately. it looks as if get is called without having set the headers
1620802396942.png


i tried with this code copying the hidden postman headers (which works)
B4X:
    Dim JobDoc As HttpJob

    JobDoc.Initialize("",Me)

    JobDoc.Download("https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100012")

    JobDoc.GetRequest.SetHeader("APIKey",apikey)

    JobDoc.GetRequest.SetHeader("APISecret",apisecret)

    JobDoc.GetRequest.SetHeader("CodiceCliente",piva_cedente)

    JobDoc.GetRequest.SetHeader("Host","webapide.aliasgrouplab.it")

    JobDoc.GetRequest.SetHeader("Connection","keep-alive")

    JobDoc.GetRequest.SetHeader("Cache-Control","no-cache")

    Wait For (JobDoc) JobDone(JobDoc As HttpJob)

    If JobDoc.Success Then

        Log("good")

        Log(JobDoc.GetString)

    Else

        Log("bad")

        Log(JobDoc.ErrorMessage)

    End If

    JobDoc.Release

    Return True
 
Upvote 0

Ale_resource

Member
Licensed User
Try
B4X:
Dim JobDoc As HttpJob
JobDoc.Initialize("",Me) ‘Just make it “”, not “JobDoc”
JobDoc.Download("https://webapide.aliasgrouplab.it/api/DocumentoAttivo/100009")
‘using a browser (safari on iPhone, the above without any extra headers just
‘Returns a blank page
Wait For (JobDoc) JobDone(JobDoc As HttpJob)
If JobDoc.Success Then
        Log(“good”)
        Log(JobDoc.GetString)
Else
        Log(“bad”)
        Log(JobDoc.ErrorMessage)
End If
JobDoc.Release ‘ don’t forget this
Note: you may need to replace the quotes (both double and single), since I’m typing this on an iPhone
This should give us a baseline to work with. First get this to work, then you can worry about headers
you get a blank page because if you don't set the header you don't have the authorization
 
Upvote 0

Ale_resource

Member
Licensed User
Show is the documentation about how to use the header when it comes to authentication.
This is the documentation
1620805331896.png


Traslate by google traslate :

To use the functions described below, you need to make HTTP / HTTPS calls using the relevant method.
To ensure secure access, all requests require an authentication token which can be obtained by initializing a session. The token must be passed in the header of the HTTP call, specifically within the Authentication key.
To ensure greater performance to calls, it is recommended to use API Keys instead of Username / Password authentication. To use this mechanism you need to generate the API keys from the DocEasy interface (API Key and API Secret) then send them in each call inserted in the HTTP call header as follows (the values are all required):
APIKey - the value of the generated APIKey key
APISecret: The generated APISecret key value
Customer Code: the VAT number of the customer for whom you want to operate (provided you have the authorization)
 
Upvote 0

Ale_resource

Member
Licensed User
trying with postman as shown in the image it works correctly, I can't understand
1620805571100.png


if on postman I disable all the headers I get an empty response, so it seems that my code is downloaded without taking into account the headers.
 
Last edited:
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Have you considered the "content-type" and "accept" headers.
Normally you would have to set these to application/json.
do these appear in the postman headers?

In the example above it appears the accept header is text/plain
 
Upvote 0

Ale_resource

Member
Licensed User
Have you considered the "content-type" and "accept" headers.
Normally you would have to set these to application/json.
do these appear in the postman headers?

In the example above it appears the accept header is text/plain
On postman I only find this as hidden =
"Accept" : "*/*"
if I set the content type
B4X:
JobDoc.GetRequest.SetContentType("application/json")
this error message appears :
java.lang.RuntimeException: Request does not support this method.
The strange thing is that if I call this api:
which does not require any parameters in the header the code works
that's why I say that in my opinion the headers that I set before downloading are not taken
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
That is correct you always set the headers AFTER the download command.

for example.
B4X:
    job.Initialize("",Me)
    Private str As String = $"${URLBase}/${currentFunction}${id}"$
    job.download(str)
    job.GetRequest.SetHeader("Authorization",$"Bearer ${currentToken}"$)
    job.GetRequest.SetHeader("accept","application/json")
    job.GetRequest.SetContentType("application/json")
    
    Wait For (job) JobDone(job As HttpJob)

This is taken from working code.
 
Upvote 0

Ale_resource

Member
Licensed User
That is correct you always set the headers AFTER the download command.

for example.
B4X:
    job.Initialize("",Me)
    Private str As String = $"${URLBase}/${currentFunction}${id}"$
    job.download(str)
    job.GetRequest.SetHeader("Authorization",$"Bearer ${currentToken}"$)
    job.GetRequest.SetHeader("accept","application/json")
    job.GetRequest.SetContentType("application/json")
   
    Wait For (job) JobDone(job As HttpJob)

This is taken from working code.
if i put
B4X:
job.GetRequest.SetContentType("application/json")
i have this error :
1620812242727.png
 
Upvote 0

Ale_resource

Member
Licensed User
trying with postman as shown in the image it works correctly, I can't understand
View attachment 113226

if on postman I disable all the headers I get an empty response, so it seems that my code is downloaded without taking into account the headers.
i tried to add this code:
B4X:
Log(JobDoc.Response.StatusCode)
and on the log appears 401 which corresponds to: Error: Unauthorized
so as I suspected, although the headers are set, they are not sent, why ??
 
Upvote 0
Top