http.get
Attempt to fetch a webpage, using the same arguments as http.request. This will return the response table or false, an error message and (optionally) a handle with the failing response’s content.
http.get Function | |
---|---|
Syntax http.get(
| |
Returns | table response | boolean false, string error[, table response] |
API | http |
Source | CC:Tweaked (source) |
The HTTP API assumes Unicode by default, meaning you may run into issues when fetching binary data. To avoid this, set binary to true. You will have to pass something to headers to set the binary flag, however just passing nil
will work fine, e.g. http.get(url, nil, true)
.
|
|||
Requests a single page and prints the contents. | |||
Code | <nowiki>
-- Make a HTTP request
local request, err = http.get("https://example.computercraft.cc")
if not request then error(err) end
-- Print the contents
print(request.readAll())
request.close() -- Don't forget to close!
</nowiki>
|
||
Output | Prints HTTP is working! |
|
|||
Requests a binary page | |||
Code | <nowiki>
-- Make a HTTP request
local request, err = http.get("https://example.computercraft.cc/logo.gif", nil, true) -- Set binary mode to true
if not request then error(err) end
-- Write the binary data to the file
local file = fs.open("logo.gif", "wb") -- Open the file in binary mode
file.write(request.readAll())
file.close() -- Close the file to save it
request.close() -- Close the HTTP request too, now that we are done with it
</nowiki>
|
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.