mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-11-16 14:54:54 +00:00
52b78f92cd
References are now written using normal links: You now use [`print`] or [print a string][`print`]) instead of @{print} or @{print|print a string}.
47 lines
1.3 KiB
Markdown
47 lines
1.3 KiB
Markdown
---
|
|
module: [kind=event] http_failure
|
|
see: http.request To send an HTTP request.
|
|
---
|
|
|
|
<!--
|
|
SPDX-FileCopyrightText: 2021 The CC: Tweaked Developers
|
|
|
|
SPDX-License-Identifier: MPL-2.0
|
|
-->
|
|
|
|
The [`http_failure`] event is fired when an HTTP request fails.
|
|
|
|
This event is normally handled inside [`http.get`] and [`http.post`], but it can still be seen when using [`http.request`].
|
|
|
|
## Return Values
|
|
1. [`string`]: The event name.
|
|
2. [`string`]: The URL of the site requested.
|
|
3. [`string`]: An error describing the failure.
|
|
4. <span class="type">[`http.Response`]|[`nil`]</span>: A response handle if the connection succeeded, but the server's
|
|
response indicated failure.
|
|
|
|
## Example
|
|
Prints an error why the website cannot be contacted:
|
|
```lua
|
|
local myURL = "https://does.not.exist.tweaked.cc"
|
|
http.request(myURL)
|
|
local event, url, err
|
|
repeat
|
|
event, url, err = os.pullEvent("http_failure")
|
|
until url == myURL
|
|
print("The URL " .. url .. " could not be reached: " .. err)
|
|
```
|
|
|
|
Prints the contents of a webpage that does not exist:
|
|
```lua
|
|
local myURL = "https://tweaked.cc/this/does/not/exist"
|
|
http.request(myURL)
|
|
local event, url, err, handle
|
|
repeat
|
|
event, url, err, handle = os.pullEvent("http_failure")
|
|
until url == myURL
|
|
print("The URL " .. url .. " could not be reached: " .. err)
|
|
print(handle.getResponseCode())
|
|
handle.close()
|
|
```
|