http.Post

From Garry's Mod
Jump to: navigation, search
 http.Post( )

Contents

Description

Sends an asynchronous POST 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)

Arguments

string url

The url to of the website to fetch.

Arguments

table parameters

The post parameters to be send to the server. Keys and values must be strings.

Arguments

function onSuccess=nil

The function called on success: function( string responseText, number contentLength, table responseHeaders, number statusCode )

Arguments

function onFailure=nil

The function called on failure: function( string errorMessage )

Arguments

table headers={}

KeyValue table for headers

Examples

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" }, function(result)
	if result then print("Done!") end
end, function(failed)
	print(failed)
end)

Output:

This is a test. Gmod Test
Personal tools
Navigation