Android Question Get specific key from the http response header

tufanv

Expert
Licensed User
Longtime User
Hello,

I have used Erel's modified code to get the http response header. I need to get the 3 keys from the response header and use those keys at the next request. The returned header is like this ( it is returned as httpjob's tag). How can I get the CloudFront-Signature,CloudFront-Key-Pair-Id and CloudFront-Policy keys from these text ?

Thanks

B4X:
{content-length=[12697], content-type=[application/json; charset=utf-8], date=[Fri, 03 May 2019 09:35:00 GMT], set-cookie=[CloudFront-Signature=U7oiYWS43uhxRouVTFpx6hrJHk7CSTGzj4V3HrDX8Pl7iuMdKJOz6r5wIIq~trJUMSsl1Z5Fe3FE-AL5kKEmnDSLH4Bi-EWXxP6PiNCw~3uB5SQq0JcfTGkn73uu9Wmz~zavdFwjqtfMV6yt8uVYU9x5JNENVULNE-lVRTRLZLuXznp1hBNcvTwTkdBmwO5X6RwEkksM4tLl~Et4DqWV~IE~7vSlhozsnZuniMTMP0X86BkKVltZNDnv5mEf9a6P1ZkVqcGWm9ENu9JTAgwQOhrdpJjP8w9QSbDU9UUVE8RhzgJVmuB3SqigD0y4C36RR9Q~PcxC0Dt1k1tQ__; Path=/; Domain=.xxx.com; Secure; HTTPOnly, CloudFront-Key-Pair-Id=APKAIVVJE7R23ILHVNCQ; Path=/; Domain=.xxx.com; Secure; HTTPOnly, CloudFront-Policy=eyJTdGF0ZW1lbnQiOiBbeyJSZXNvdXJjZSI6Imh0dHBzOi8vbmV3cy1jb250ZW50LmZvcmVrcy5jb20vKiIsIkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOwb2NoVGltZSI6MTU1Njg3OTcwMH19fV19; Path=/; Domain=.xxx.com; Secure; HTTPOnly]}
 

DonManfred

Expert
Licensed User
Longtime User
Where is your code? Do not expect everyone know which code you exactly did changed or used.
Also note that the okhttputils does provide a Resultobject with the headers. Did you checked them?

B4X:
Sub JobDone(Job As HttpJob)
    HideKeyboard
    ProgressDialogHide
    If Job.Success Then
        Dim resp As OkHttpResponse = Job.Response
        Dim headers As Map  = resp.GetHeaders
        For Each key As String In headers.Keys
            Log($"Header: ${key} = ${headers.Get(key)}"$)
        Next
 
Upvote 0

MathiasM

Active Member
Licensed User
What have you tried? What didn't work?
Those keys you mention are in the text, so you only need to extract them, you can use different methods to do so. Regex being the hardest (imho) and simple string splitting being the most straight forward.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
In fact, what I meant was as DonManfred stated, is there a resultobject but as you say, it is a simple text, and what DonManfred suggested works perfect here. I couldn't extract it from the text.
What have you tried? What didn't work?
Those keys you mention are in the text, so you only need to extract them, you can use different methods to do so. Regex being the hardest (imho) and simple string splitting being the most straight forward.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Where is your code? Do not expect everyone know which code you exactly did changed or used.
Also note that the okhttputils does provide a Resultobject with the headers. Did you checked them?

B4X:
Sub JobDone(Job As HttpJob)
    HideKeyboard
    ProgressDialogHide
    If Job.Success Then
        Dim resp As OkHttpResponse = Job.Response
        Dim headers As Map  = resp.GetHeaders
        For Each key As String In headers.Keys
            Log($"Header: ${key} = ${headers.Get(key)}"$)
        Next
I said working well and it is but problem now is I get the headers' keys and it is like this: The things I need is in a single key so Should I use text splitting or regex to get those, I think there is no other way to get it.

B4X:
Waiting for debugger to connect...
Program started.
Header: content-length = [15558]
Header: content-type = [application/json; charset=utf-8]
Header: date = [Fri, 03 May 2019 10:34:06 GMT]
Header: set-cookie = [CloudFront-Signature=PxOhQCxfhCVNfAr72SRETyTy72BGAdr4yT5jHhj8v4dSzsb4au0eDVfvyQVx15Ml4sjjvLim~lgsW3wQu3~4PxFTMqiA-jV8e144oy37r~j1y1zRMz4anKMpTG6TybS65IkO4rXdVK1Fok3mhy8Y~-b5QgyiUE3LUIHzv3XaTHHRLKBlTLWDk2GscKGPhhM6wtOZ8SnThnqaMfgtSjG9slepk9DnqLV7WRDcrAGnkJ-~K-NLx2lBb7BzzUEG1c0ii49CDhB5FcdF1vh9LiaaydJhwMrebzwGyM-uqu4UlOdfSnW~2HD7RjNT9z6l1h4QSZOt2rq~H3SgX3t8yGMzdg__; Path=/; Domain=.xxx.com; Secure; HTTPOnly, CloudFront-Key-Pair-Id=APKAIVVJE7R23ILHVNCQ; Path=/; Domain=.xxx.com; Secure; HTTPOnly, CloudFront-Policy=eyJTdGF0ZW1lbnQiOiBbeyJSZXNvdXJjZSI6Imh0dHBzOi8vbmV3cy1jb250ZW50LmZvcmVrcy5jb20vKiIsIkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTU1Njg4MzI0Nn19fV19; Path=/; Domain=.xxx.com; Secure; HTTPOnly]

by the way Erel's modified code is here : https://www.b4x.com/android/forum/threads/httputils2-getheaders.45051/#content
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
try to get the header set-cookie from the map. Try to cast it to a map instead of a string.

Note that the Cookies are a List.
1. get headers
2. get the list set-cookie from the headermap.
3. iterate through the List. Maybe the items in the list are MAPS. You need to try

At least you should now have a string which you could parse using regex....
 
Last edited:
Upvote 0

tufanv

Expert
Licensed User
Longtime User
try to get the header set-cookie from the map. Try to cast it to a map instead of a string.

Note that the Cookies are a List.
1. get headers
2. get the list set-cookie from the headermap.
3. iterate through the map. Maybe the items in the list are MAPS. You need to try

At least you should now have a string which you could parse using regex....
Thanks !
 
Upvote 0
Top