<?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_1";
//]]></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_request</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_request"/>
<meta name="rh-index-keywords" content="http_request"/>
<meta name="search-keywords" content="http_request"/>
</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_request">
<span>http_request</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_request</h1>
<p>With this function, you can create an HTTP header request to define the operating parameters of an HTTP transaction, which can be used for many things like (for example) authentication via HTTP headers if you use RESTful APIs. The function requires
the full IP address of the server to request from as well as the type of request to make (as a string, see the note below): "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "OPTIONS",
or "CONNECT". You will also need to supply a <tt><a href="../../Data_Structures/DS_Maps/DS_Maps.htm">DS map</a></tt> of key/value pairs (as strings, where the key is the header field and the value is the required data for the header), and
the final argument is an optional data string that you can add to the request, and if it's not needed then it can be either 0 or an empty string "". Note that you can also send a buffer (see the section on <a href="../../Buffers/Buffers.htm">Buffers</a> for more details), in which case the last argument would be the "handle" of the buffer to send.</p>
<p class="note"><b>NOTE</b>: HTTP headers normally follow the format "key:value", but since GMS creates these pairs for you from the ds_map, there is no need to include the colon ":" in your map key or value strings.</p>
<p class="note"><b>NOTE</b>: When using a buffer for the body argument, if the buffer seek position is at the start (0) then no body is sent and the buffer is filled with the response from the http call, but if the buffer seek position is non-zero, then the buffer
string content is sent as the body.</p>
<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 part on <a href="HTTP.htm">Cross Domain Issues</a> for further details.</p>
<p>This event will generate a "call back" which is picked up by any <a href="../../../../The_Asset_Editors/Object_Properties/Async_Events/HTTP.htm">HTTP Events</a>, in which case it will generate a <tt>DS Map</tt> (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 has the following two keys related to the request function:</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>response_headers:</b> If this has a value greater than or equal to 0, it holds a ds_map that contains the HTTP headers returned with the response to the HTTP request.</li>
</ul>
<p> </p>
<h4>Syntax:</h4>
<p class="code">http_request(url, method, header_map, body);</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>
<tr>
<td>method</td>
<td>The request method (normally "POST" or "GET", but all methods are supported)</td>
</tr>
<tr>
<td>header_map</td>
<td>A ds_map with the required header fields</td>
</tr>
<tr>
<td>body</td>
<td>The data to be transmitted following the headers (can be a binary buffer handle)</td>
</tr>
</tbody>
</table>
<p> </p>
<h4>Returns:</h4>
<p class="code">Real</p>
<p> </p>
<h4>Example:</h4>
<p class="code">var map = ds_map_create();<br/> ds_map_add(map, "Host", "225.0.0.97:3000");<br/> ds_map_add(map, "Connection", "keep-alive");<br/> ds_map_add(map, "Content-Length", "169");<br/> ds_map_add(map,
"Cache-Control", "max-age=0");<br/> ds_map_add(map, "Authorization", "Basic eW95b19hZG1pbjpjNG5lZmllbGQ=");<br/> ds_map_add(map, "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");<br/> ds_map_add(map, "User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36");<br/> ds_map_add(map, "Content-Type", "application/x-www-form-urlencoded");<br/> ds_map_add(map, "Accept-Encoding", "gzip,deflate,sdch");<br/> ds_map_add(map, "Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");<br/> ds_map_add(map, "Cookie", "request_method=GET; _InAppPurchases_session=69bb6ef6eec2b");<br/> var data="utf8=%E2%9C%93&authenticity_token=kPmS14DcYcuKgMFZUsN3XFfj3mhs%3D&product%5Bname%5D=CatchTheHaggis&product%5Bproduct_id%5D=http_test&commit=Create+Product";<br/> request = http_request("http:225.0.0.97:3000/products",
"POST", map, data);</p>
<p>The above code creates a DS map with the relevant HTTP headers for the function, then creates a data string for use as this is a POST request. Finally the function is called, with it's ID value being stored in the variable "request" for
checking in the HTTP asynchronous event.</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</a></div>
</div>
</div>
<h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
</div>
<!-- KEYWORDS
http_request
-->
<!-- TAGS
http_request
-->
</body></html>