This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TehTotalPwnage's profile picture

Reading HTTP Response Headers?

Started by TehTotalPwnage, 22 December 2015 - 01:42 AM
TehTotalPwnage #1
Posted 22 December 2015 - 02:42 AM
Title basically says it all. If I make an HTTP request in ComputerCraft, how can I get any headers that it returns such as the Location header for 301 redirects, the content length, and so on.
KingofGamesYami #2
Posted 22 December 2015 - 02:53 AM
handle.getResponseCode()
TehTotalPwnage #3
Posted 22 December 2015 - 03:40 AM
As far as I know, getResponseCode() only returns the HTTP status code such as 200 OK or 301 Moved Permanently, but doesn't display the rest of the response headers.
KingofGamesYami #4
Posted 22 December 2015 - 04:27 AM
In that case, there is no way to get any other response header.
Bomb Bloke #5
Posted 22 December 2015 - 04:37 AM
I've got a memory that the betas for CC 1.76 can do some extra stuff in this area? The http API isn't something I've put a lot of research into.
Creator #6
Posted 22 December 2015 - 12:32 PM
For content length, just do the following:


local f = http.get(url)
local length = #f.readAll()

f.readAll returns the text on the page you requested.

and "#" return the number of elements in an object.
Lyqyd #7
Posted 22 December 2015 - 03:30 PM
I'd point out fs.getSize before such awkward workarounds.
Creator #8
Posted 22 December 2015 - 03:55 PM
I'd point out fs.getSize before such awkward workarounds.

It is for http requests, not for the filesystem.
Lyqyd #9
Posted 22 December 2015 - 05:00 PM
So it is. Which makes your suggestion even worse, because now the content must be fetched a second time to do anything useful with it. At least suggest saving it to a separate string before finding the length!