Reading Response Headers

Is there any way to read Response Headers from a POST operation? In my app I am needing to pull some data from the Response Headers (Response Content is empty) but am unable to find any way to do this. Any help is appreciated.

Please provide a more detailed explanation of what you are trying to achieve,
what you have done so far, what works, what doesn’t work…

Please show your relevant blocks,
if relevant, your data or lists,
screenshots of the Designer screen or app in action,
so we can see what you are trying to do…

Right Click in Blocks Editor and select “Download Blocks as Image”
for the best image of your blocks.

Hello,
I have exactly the same problem, I trying to get the Response Header from a POST request. I have to send a POST request to an IP, and in the body request I put my credentials. In the response header I get 2 variables, which I need to done my app. Attached a screenshot of my program. (The POST request is at the right of the program, named : “sendCommands.Click”)
Best regards

1 Like

What response to you get from curl or postman (and what do you send…) ?

I send this curl command:

curl “http://192.168.1.1/ws” -H “User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0” -H “Accept: /” -H “Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3” --compressed -H “Authorization: X-Sah-Login” -H “Content-Type: application/x-sah-ws-4-call+json” -H “Origin: 192.168.1.1” -H “Connection: keep-alive” -H “Referer: 192.168.1.1” -H “Cookie: 3d857f51/accept-language=fr,fr-FR; UILang=fr” --data “{”“service”":"“sah.Device.Information”","“method”":"“createContext”","“parameters”":{"“applicationName”":"“webui”","“username”":"“admin”","“password”":""*******""}}" .

And I get in the response header :

{“En-têtes de la réponse (223 o)”:{“headers”:[{“name”:“Cache-Control”,“value”:“no-cache”},{“name”:“Content-Type”,“value”:“application/x-sah-ws-4-call+json”},{“name”:“Pragma”,“value”:“no-cache”},{“name”:“Set-Cookie”,“value”:“3d857f51/sessid=0z1F2tK2gjQAbw/9a8baoKCF; path=/; HttpOnly”},{“name”:“TE”,“value”:“chunked”},{“name”:“Transfer-Encoding”,“value”:“chunked”}]}}.

The things I need is to get the cookie value, which is in the request header. Thank you.

So you want to locate and extract the “3d857f51/sessid=0z1F2tK2gjQAbw/9a8baoKCF; path=/; HttpOnly” value? You have to iterate (for each) over the list of headers and see which header has “name”:“Set-Cookie” in it, then look in that header for tag "value", whose value will be what you want.

Yes, it’s exactly what I want to do, I want to get that : 3d857f51/sessid=0z1F2tK2gjQAbw/9a8baoKCF . But I don’t know how to get the request header, that’s my problem :slight_smile: You say I have to iterate (for each) over the list of headers, but I don’t know how I can do it. Can you help me ?

You have to work on the web response from the outside in,
saving intermediate results in global variables as stepping stones to help spot where you fail.

From the outside in, you have a JSONresponse object with tag “En-têtes de la réponse (223 o)” and value an object with headers a list under tag “headers”.
global responseTag global webResponse

I just tried both Web Jsondecode blocks on it, and got back empty texts, so we will have to fix the text, (On further inspection the JSON text you posted ended up with non-JSON slanty quotes, which need to be replaced with straight quotes.)
straighten

Here’s the meat …

and the proof:
Capture
and the .aia file:
JSON_headers.aia (4.5 KB)

Hello, thanks a lot for your work, but my problem is still not solved. You wrote into the variable “webResponse” directly the response, but I need to get it from the response header, updated each time I start the app. The cookie credentials I get is available only during 15 minutes. Can you tell me how I can get the header, without writing it into a variable ? Thank you

You would need some one who actually knows about http headers for that.
I only know about JSON parsing.

If the headers change at each run, ask for them in Screen1.Initialize.

P.S. What do you have against variables?

Hello,

I don't know if this will work for you ... in this discussion there is an extension called: com.KIO4_Terminal.aix

With this extension you can use many LINUX commands
Some devices have the curl command.
Can you test if this instruction works for you?
Terminal.Run
commandLine: curl -I https://www.keycdn.com

from Popular curl Examples - KeyCDN Support

Hello, everyone ! I think I haven’t explained well my problem. I want to perform a PUT request to a server to get a cookie credentials, which is in the header of the response. The PUT request works well on my program, I can send the request, and I get the body of the response. That’s my problem. I don’t know if it’s possible to get the response header in AppInventor, and if it is, tell me how to do it. Thank you.

try, if the extension from @Juan_Antonio can help...
the web component unfortunately does not provide a functionality to get response headers

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by Taifun.

Hello, this extension can’t work for me, I want something automatic, if I send commands, it won’t be the program I want :).

Does it return the headers ?

Other header response curl command examples:

curl -s -D - https://api.github.com/users | less
curl -i https://api.github.com/users | less
curl -IL http://bit.ly/in-the-shadows
curl -X HEAD -I http://httpbin.org/
curl -sSL -D - www.acooke.org -o /dev/null
curl -X POST http://httpbin.org/post -v > /dev/null
curl -X POST -sSL -D - www.acooke.org -v > /dev/null

Hello, I don’t know why you want I send curl commands, in my program I never sends curl commands.

It was you who asked me to curl the requested url, from my computer. But in my program, I never use curl.

Because the web component cannot return header responses from a server, instead you could possibly use Juan’s terminal extension to send a curl command instead, which might return the header responses you are after. You would need to have curl in your /system/bin for this to work.

well, the web component is not able to do what you want
your only option as far as I know is to try the extension as already mentioned together with the corresponding curl command
you might want to try it... it is your choice...

Taifun