Garry's Mod Wiki

http.Post

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

Description

Sends an asynchronous POST request to an HTTP server. Internally calls HTTP.

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).

This cannot send or receive multiple headers with the same name.

Issue Tracker: 2232
HTTP-requests that respond with a large body may return an unsuccessful error. Try using the Range header to download the file in chunks.
HTTP-requests to destinations on private networks (such as 192.168.0.1, or 127.0.0.1) won't work.
To enable HTTP-requests to destinations on private networks use Command Line Parameters -allowlocalhttp. (Dedicated servers only)

Arguments

1 string url
The url to of the website to post.
2 table parameters
The post parameters (x-www-form-urlencoded) to be send to the server. Keys and values must be strings.
3 function onSuccess = nil
Function to be called on success.
Function argument(s):
1 string body - The reponse body as a string.
2 string size - equal to string.len(body).
3 table headers - The response headers as a table.
4 number code - The HTTP success code.
4 function onFailure = nil
Function to be called on failure.
Function argument(s):
1 string error - The error message.
5 table headers = {}
KeyValue table for headers.

Example

Write a file in PHP, and invoke it from Lua. The output below is written in the file, not in the console.

<?php $p = $_POST["p"]; $a = $_POST["a"]; $f = fopen("write.html", "w"); fwrite($f, "This is a test. $p $a\n"); fclose($f); ?>
http.Post( "http://localhost/post.php", { p = "Gmod", a = "Test" }, -- onSuccess function function( body, length, headers, code ) print( "Done!" ) end, -- onFailure function function( message ) print( message ) end )
Output:
This is a test. Gmod Test