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 ?
 

mcqueccu

Well-Known Member
Licensed User
Longtime User
Its not solved yet. It worked briefly I realised it worked because of something. Am still debugging and will bring the result out


I tried the code in B4J, using jOKhttputils2. The code ONLY works if these two things are done.

1. Add Conditional symbol (HU2_ACCEPTALL) to the build configurations and
2. When a software called HTTP debugger is opened (https://www.httpdebugger.com/)

So, when the http dubugger software on the machine is closed, it gives 401, but when its opened and running, I get 200 response>

There may be something either in the okhttputils or the API not right
 
Upvote 0

Ale_resource

Member
Licensed User
Hello, I understand the problem. Practically the library sends the Headers all in lowercase for example APISecret is sent as apisecret and on the server side they are case sensitive.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Hello, I understand the problem. Practically the library sends the Headers all in lowercase for example APISecret is sent as apisecret and on the server side they are case sensitive.
Then I think this is a problem with the API since to my knowledge, header names are case insensitive.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Just tried the case sensitivity in Postman. Yes, it looks like the APIKey header needs to be the exact case given. The other 2 are fine.

This works:


This does not:
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I don't think there is an easy solution to this from B4x side.

Looking at the source for okHttp on git hub.



there is the following:
B4X:
/**
         * Sets the value of the header with the given name. If no such header exists then a new header will be added.
         */
        public void SetHeader(String Name, String Value) {
            builder.addHeader(Name, Value);
        }

So if I have read this correctly it seems to be the base HttpRequest java code which is lowercasing the header value. As the value given is just passed without change.

The developer should really change their API to be correct. The only other solution I can see is to use something as a buffer which can make case sensitive calls.
It may be possible to inject some javascript into a webview or use PHP on a server.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Hello, I understand the problem. Practically the library sends the Headers all in lowercase for example APISecret is sent as apisecret and on the server side they are case sensitive
Are you sure. Now, I'm using B4J to test this, but I'm having the same issues with OP's site. Here is the Wireshark capture for a request created in B4J to PTSV2
As you can see, no lowercasing was performed by the okhttp3 library
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Interesting. All I can say is that in my tests with PostMan changing the "APIKey" header to "APIkey" causes the 401 error.
Making APISecret and CodiceCliente lowercase or original case has no effect - the call still works until a change to APIKey is made.
 
Upvote 0

Ale_resource

Member
Licensed User
Are you sure. Now, I'm using B4J to test this, but I'm having the same issues with OP's site. Here is the Wireshark capture for a request created in B4J to PTSV2

As you can see, no lowercasing was performed by the okhttp3 library
yes, i'm sure .. i was also confirmed by the programmer who created the API that there is a casesensitive check on the apikey header
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
casesensitive check on the apikey header
That’s an issue with the API and the developer, since by standard headers are case insensitive
 
Upvote 0

Ale_resource

Member
Licensed User
yes I know, that's what I told the programmer .. who replied that no one has ever had problems and so I have to adapt
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
who replied that no one has ever had problems and so I have to adapt
What I don’t get is why wireshark shows that the case is not changed, yet access still does not work. Almost like it is an issue on his side
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…