Jump to content




Reading HTTP Response Headers?


  • You cannot reply to this topic
8 replies to this topic

#1 TehTotalPwnage

  • Members
  • 15 posts

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.

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 22 December 2015 - 02:53 AM

handle.getResponseCode()


#3 TehTotalPwnage

  • Members
  • 15 posts

Posted 22 December 2015 - 03:40 AM

View PostKingofGamesYami, on 22 December 2015 - 02:53 AM, said:

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.

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 22 December 2015 - 04:27 AM

In that case, there is no way to get any other response header.

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#6 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

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.

#7 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 22 December 2015 - 03:30 PM

I'd point out fs.getSize before such awkward workarounds.

#8 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 22 December 2015 - 03:55 PM

View PostLyqyd, on 22 December 2015 - 03:30 PM, said:

I'd point out fs.getSize before such awkward workarounds.

It is for http requests, not for the filesystem.

#9 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users