<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<script type="text/javascript" language="JavaScript">
//<![CDATA[
function reDo() {
if (innerWidth != origWidth || innerHeight != origHeight)
location.reload();
}
if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
origWidth = innerWidth;
origHeight = innerHeight;
onresize = reDo;
}
onerror = null;
//]]>
</script>
<style type="text/css">/*<![CDATA[*/
< !-- div.WebHelpPopupMenu {
position: absolute;
left: 0px;
top: 0px;
z-index: 4;
visibility: hidden;
}
p.WebHelpNavBar {
text-align: right;
}
-->
/*]]>*/</style>
<script type="text/javascript">//<![CDATA[
gRootRelPath = "../../../..";
gCommonRootRelPath = "../../../..";
gTopicId = "9.2.12.1.0_2";
//]]></script>
<script type="text/javascript" src="../../../../template/scripts/rh.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/common.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/topic.min.js"></script>
<script type="text/javascript" src="../../../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../../../whxdata/projectsettings.js"></script>
<link rel="stylesheet" type="text/css" href="../../../../template/styles/topic.min.css"/>
<link rel="stylesheet" type="text/css" href="../../../../template/Charcoal_Grey/topicheader.css"/>
<meta name="topic-status" content="Draft"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>http_get</title>
<meta name="generator" content="Adobe RoboHelp 2019"/>
<link rel="stylesheet" href="../../../../assets/css/default.css" type="text/css"/>
<meta name="rh-authors" content="Mark Alexander"/>
<meta name="topic-comment" content="Reference page for http_get"/>
<meta name="rh-index-keywords" content="http_get"/>
<meta name="search-keywords" content="http_get"/>
</head>
<body>
<div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
<div class="logo">
</div>
<div class="nav">
<div class="title" title="http_get">
<span>http_get</span>
</div>
<div class="gotohome" title="Click here to see this page in full context">
<span>Click here to see this page in full context</span>
</div>
</div>
</div>
<div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>
<!--<div class="body-scroll" style="top: 150px;">-->
<h1>http_get</h1>
<p>With this function, you connect to the specified URL in order to retrieve information. As this is an asynchronous function, GameMaker Studio 2 will not block while waiting for a reply, but will keep on running unless it gets callback information.
This information will be in the form of a string and will trigger an <b>Async Event</b> in any instance that has one defined in their object properties. You should also note that HTTP request parameters (the bits sometimes "tacked on" to
the end of a URL when you submit a form on a web page) are perfectly acceptable when using this function, for example:</p>
<p class="code">http_get("http://www.YoYoGames.com/logon?username="+name);</p>
<p>will pass the string held in the variable "name" to the server as well a retrieve the data from that URL. So, essentially, any time a simple, short piece of data needs to be passed from the client to the server, this would be reasonable choice
as the function to use.</p>
<p>This function will generate multiple "call backs" which are picked up by any <a href="../../../../The_Asset_Editors/Object_Properties/Async_Events/HTTP.htm">HTTP Events</a>. These will generate a <a href="../../Data_Structures/DS_Maps/DS_Maps.htm"><tt>DS Map</tt></a> (more commonly known as a "dictionary") that is exclusive to this event and is stored in the special variable <a href="../../../GML_Overview/Variables/Builtin_Global_Variables/async_load.htm"><b>async_load</b></a>. This DS map will contain
different values depending on whether there is data being returned or not. For example, if you have requested a file, the event will trigger multiple times as each packet of data is received so that you can show a progress bar, for example. The general
structure for the DS map will be as follows:</p>
<ul class="colour">
<li><b>id: </b>The ID which was returned from the command. If you fire off a series of <tt>http_</tt> requests then you need to know which one you are getting the reply to, and so you would use this value to compare to the value you stored when you originally
sent the request to find the right one.</li>
<li><b>status: </b>Returns a value of less than 0 for an error, 0 for complete and 1 for receiving packets (see below for more details).</li>
<li><b>result: </b>The data received (string only).</li>
<li><b>url: </b>The complete URL you requested.</li>
<li><b>http_status: </b>The raw http status code (if available). This returns the standard web status code for most browsers, eg: 304 for "Not Modified" or 204 for "No Content", etc...</li>
</ul>
<p>If there are multiple packets being returned to your game, the callback "status" key will return 1, in which case the ds_map will have the following additional keys:</p>
<ul class="colour">
<li><b>"contentLength": </b>This is the size of file that the web server has said you should expect to receive (may be -1 if the server does not return this data).</li>
<li><b>"sizeDownloaded": </b>The size of the data that has already been downloaded.</li>
</ul>
<p class="note"><b>NOTE</b>: You should be aware that due to XSS protection in browsers, requests to and attempts to load resources from across domains are blocked and may appear to return blank results. Please see the section on <a href="HTTP.htm">Cross Domain Issues</a> for further details.</p>
<p> </p>
<h4>Syntax:</h4>
<p class="code">http_get(url);</p>
<table>
<tbody>
<tr>
<th>Argument</th>
<th>Description</th>
</tr>
<tr>
<td>url</td>
<td>The web address of the server that you wish to get information from</td>
</tr>
</tbody>
</table>
<p> </p>
<h4>Returns:</h4>
<p class="code">Real</p>
<p> </p>
<h4>Extended Example:</h4>
<p>The <tt>http_get()</tt> function can be called from any event, and since it is asynchronous the callback can be almost instantaneous or could take several seconds. Calling the function is simple and would look something like this:</p>
<p class="code">get = http_get("http://www.MacSweeneyGames.com/logon?username=" + name);</p>
<p>The above code will pass the string held in the variable "name" to the given server as well as retrieve the data from that URL, triggering an Async Event which will contain the async_load DS map (the async_load map index will be stored in
the variable "get" so you can check for the correct callback). The Async Event triggered would be the <b>HTTP</b> sub-event, and in that event you would have the following:</p>
<p class="code">if ds_map_find_value(async_load, "id") == get<br/> {
<br/> if ds_map_find_value(async_load, "status") == 0<br/> {
<br/> r_str = ds_map_find_value(async_load, "result");<br/> }
<br/> else
<br/> {
<br/> r_str = "null";<br/> }
<br/> }
</p>
<p>The above code will first check the id of the DS map that has been created, then check the "status" of the callback. If the value is equal to 0 (signalling success) the result from the callback will then be stored in a variable for future
use, otherwise the variable will be set to a default value (in this case "null").</p>
<p> </p>
<p> </p>
<p> </p>
<div class="footer">
<div class="buttons">
<div class="clear">
<div style="float:left">Back: <a href="HTTP.htm">HTTP</a></div>
<div style="float:right">Next: <a href="http_get.htm">http_get_file</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
<!-- KEYWORDS
http_get
-->
<!-- TAGS
http_get
-->
</body></html>