Garry's Mod Wiki

Revision Difference

http.Post#546211

<function name="Post" parent="http" type="libraryfunc"> <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 <page text="onSuccess">Structures/HTTPRequest</page> callback. The <page text="onFailure">Structures/HTTPRequest</page> callback is usually only called on DNS or TCP errors (e.g. the website is unavailable or the domain does not exist). ⤶ <bug>Currently there is a problem with CURL library on Linux being unable to validate HTTPS connections with certificates issued by Let's Encrypt. The best course of action to solve this problem now is to use binary module that replaces HTTP function. Either this one https://github.com/WilliamVenner/gmsv_reqwest or this one https://github.com/timschumi/gmod-chttp.</bug>⤶ <note>HTTP-requests to destinations on private networks (such as `192.168.0.1`) won't work.<br/> To enable HTTP-requests to destinations on private networks use <page>Command Line Parameters</page> `-allowlocalhttp` (serverside only).</note>⤶ ⤶ <bug issue="5065">Currently there is a problem with CURL library on Linux being unable to validate HTTPS connections with certificates issued by Let's Encrypt. The best course of action to solve this problem now is to use binary module that replaces HTTP function. Either this one https://github.com/WilliamVenner/gmsv_reqwest or this one https://github.com/timschumi/gmod-chttp.⤶ **This is fixed in the next update**.</bug>⤶ ⤶ <note>HTTP-requests to destinations on private networks (such as `192.168.0.1`) won't work.<br/> To enable HTTP-requests to destinations on private networks use <page>Command Line Parameters</page> `-allowlocalhttp` (serverside only).</note>⤶ </description> <realm>Shared and Menu</realm> <file line="46-L73">lua/includes/modules/http.lua</file> <args> <arg name="url" type="string">The url to of the website to post.</arg> <arg name="parameters" type="table">The post parameters (x-www-form-urlencoded) to be send to the server. **Keys and values must be strings**.</arg> <arg name="onSuccess" type="function" default="nil">Function to be called on success. Arguments are * <page>string</page> body * <page>string</page> size - equal to <page>string.len</page>(body). * <page>table</page> headers * <page>number</page> code - The HTTP success code.</arg> <arg name="onFailure" type="function" default="nil">Function to be called on failure. Arguments are * <page>string</page> error - The error message.</arg> <arg name="headers" type="table" default="{}">KeyValue table for headers.</arg> </args> </function> <example> <description> 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); ?> ``` </description> <code> 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 ) </code> <output>This is a test. Gmod Test</output>⤶ <output>⤶ ```⤶ This is a test. Gmod Test⤶ ```⤶ </output>⤶ </example>