Android Question Set Cookie with HttpJob

Jerez

Active Member
Licensed User
Longtime User
Hi.

I need help with this, please.

This is my code to get a json string that works ok on my local network if i previously logged in locally (In Chrome for example).

But in order to work without login in a browser i need to set a cookie first. How can i do that?

How can i set a Cookie then save it and reuse in every next downloads?

This is my current code:

B4X:
Dim job As HttpJob

If Not (job.IsInitialized) Then
              
    job.Initialize("GET_DEVICES", Me)
              
End If
          
job.Download(Server & "/api/v2/devices/1")
job.GetRequest.Timeout = 6000000


This is the API documentation in order to login with cookies:

B4X:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"form": true, "login": "admin", "password": "admin", "keepme": false, "default_ui": 1}' 192.168.0.62/api/v2/login -c cookie.txt

Now we can execute with cookie:
B4X:
curl 192.168.0.62/api/v2/devices/1 -b cookie.txt

Thanks for advise!

mcdonalds-Chocolate-Chip-Cookie.png
 
Last edited:

Jerez

Active Member
Licensed User
Longtime User
Cookies are set in the headers.
B4X:
Job.Download(...)
Job.GetRequest.SetHeader("Cookie", "cookie data")

Ok but in this case i need to download the 192.168.0.62/api/v2/login pass parameters as json/text, then get the response in job_done and then setHeader in next downloads?
 
Upvote 0

Jerez

Active Member
Licensed User
Longtime User
If the cookie is set by the server then it will happen automatically. Otherwise you need to manually add it to the downloads.
Yeah!, thanks man! yesterday i've attached the cookie on every download and works fine!
 
Upvote 0
Top