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 ds_map 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 Buffers for more details), in which case the last argument would be the "handle" of the buffer to send.
This event will generate a "call back" which is picked up by any HTTP Events, in which case it will generate a ds_map (more commonly known as a "dictionary") that is exclusive to this event and is stored in the special variable async_load. This ds_map has the following two keys related to the request function:
- id: The ID which was returned from the command. If you fire off a series of http_ 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.
- response_headers: 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.
NOTE: 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.NOTE: 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.NOTE: 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 Cross Domain Issues for further details.
http_request(url, method, header_map, body);
参数 描述 url The web address of the server that you wish to get information from method The request method (normally "POST" or "GET", but all methods are supported) header_map A ds_map with the required header fields body The data to be transmitted following the headers (can be a binary buffer handle)
var map = ds_map_create();
ds_map_add(map, "Host", "22.214.171.124:3000");
ds_map_add(map, "Connection", "keep-alive");
ds_map_add(map, "Content-Length", "169");
ds_map_add(map, "Cache-Control", "max-age=0");
ds_map_add(map, "Authorization", "Basic eW95b19hZG1pbjpjNG5lZmllbGQ=");
ds_map_add(map, "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
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");
ds_map_add(map, "Content-Type", "application/x-www-form-urlencoded");
ds_map_add(map, "Accept-Encoding", "gzip,deflate,sdch");
ds_map_add(map, "Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");
ds_map_add(map, "Cookie", "request_method=GET; _InAppPurchases_session=69bb6ef6eec2b");
request = http_request("http:126.96.36.199:3000/products", "POST", map, data);
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.