2021-01-19 09:20:52 +00:00
|
|
|
---
|
|
|
|
module: [kind=event] http_failure
|
|
|
|
see: http.request To send an HTTP request.
|
|
|
|
---
|
|
|
|
|
2023-03-15 21:52:13 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: 2021 The CC: Tweaked Developers
|
|
|
|
|
2023-03-28 09:23:28 +00:00
|
|
|
SPDX-License-Identifier: MPL-2.0
|
2023-03-15 21:52:13 +00:00
|
|
|
-->
|
|
|
|
|
2023-08-24 09:48:30 +00:00
|
|
|
The [`http_failure`] event is fired when an HTTP request fails.
|
2021-01-19 09:20:52 +00:00
|
|
|
|
2023-08-24 09:48:30 +00:00
|
|
|
This event is normally handled inside [`http.get`] and [`http.post`], but it can still be seen when using [`http.request`].
|
2021-01-19 09:20:52 +00:00
|
|
|
|
|
|
|
## Return Values
|
2023-08-24 09:48:30 +00:00
|
|
|
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
|
2023-01-16 22:53:58 +00:00
|
|
|
response indicated failure.
|
2021-01-19 09:20:52 +00:00
|
|
|
|
|
|
|
## Example
|
|
|
|
Prints an error why the website cannot be contacted:
|
|
|
|
```lua
|
2021-01-23 14:58:08 +00:00
|
|
|
local myURL = "https://does.not.exist.tweaked.cc"
|
2021-01-19 09:20:52 +00:00
|
|
|
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()
|
|
|
|
```
|