Garry's Mod Wiki

http.Fetch

http.Fetch

http.Fetch( string url, function onSuccess = nil, function onFailure = nil, table headers = {} )

Description

Launches an asynchronous GET request to a HTTP server.

HTTP requests returning a status code >= 400 are still considered a success and will call the onSuccess callback.

The onFailure callback is usually only called on DNS or TCP errors (e.g. the website is unavailable or the domain does not exist).

A rough overview of possible onFailure messages:

  • invalid url - Invalid/empty url ( no request was attempted )
  • invalid request - Steam HTTP lib failed to create a HTTP request
  • error - OnComplete callback's second argument, bError, is true
  • unsuccessful - OnComplete's first argument, pResult->m_bRequestSuccessful, returned false
HTTP-requests to destinations on private networks (such as 192.168.0.1) won't work.
To enable HTTP-requests to destinations on private networks use Command Line Parameters -allowlocalhttp (serverside only).

Arguments

1 string url
The URL of the website to fetch.
2 function onSuccess = nil
Function to be called on success. Arguments are

3 function onFailure = nil
Function to be called on failure. Arguments are

  • string error - The error message.
4 table headers = {}
KeyValue table for headers.

Example

Shows the typical usage to get the HTML of a webpage.

local theReturnedHTML = "" -- Blankness http.Fetch( "https://www.google.com", -- onSuccess function function( body, length, headers, code ) -- The first argument is the HTML we asked for. theReturnedHTML = body end, -- onFailure function function( message ) -- We failed. =( print( message ) end, -- header example { ["accept-encoding"] = "gzip, deflate", ["accept-language"] = "fr" } )
Output: If it successfully fetched the page, the variable theReturnedHTML should contain the returned HTML in plain text.

Special Pages


Wikis

?

Render Time: 29ms

DB GetPage 3
Generate Html 11
SaveChanges (1) 7
Render Body 0
Render Sidebar 6