HTTP

From SA-MP Wiki

Jump to: navigation, search


Description:

Sends a threaded HTTP request.


HTTP was added in SA-MP 0.3b This function was added in SA-MP 0.3b and will not work in earlier versions!


Parameters:
(index, type, url[], data[], callback[])


indexID used to differentiate requests that are sent to the same callback (useful for playerids)
typeThe type of request you wish to send.
url[]The URL you want to request. (Without 'http://')
data[]Any POST data you want to send with the request.
callback[]Name of the callback function you want to use to handle responses to this request.

Return Values:

1 on success, 0 on failure


Request types

HTTP_GET - Sends a regular HTTP request.

HTTP_POST - Sends a HTTP request with POST data.

HTTP_HEAD - Sends a regular HTTP request, but ignores any response data - returning only the response code.

Response Codes

HTTP_ERROR_BAD_HOST			1
HTTP_ERROR_NO_SOCKET			2
HTTP_ERROR_CANT_CONNECT	                3
HTTP_ERROR_CANT_WRITE			4
HTTP_ERROR_CONTENT_TOO_BIG		5
HTTP_ERROR_MALFORMED_RESPONSE	        6
Image:32px-Ambox_warning_orange.png

Note

As well as the response codes listed above, there are also all of the typical HTTP responses such as 404 (Page not found), 500 (Server error) or 403 (forbidden)


Example Usage:

forward MyHttpResponse(index, response_code, data[]);
 
public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp("/hello",cmdtext,true))
    {
        HTTP(playerid, HTTP_GET, "kc.gd/hello.txt", "", "MyHttpResponse");
        return 1;
    }
    return 0;
}
 
public MyHttpResponse(index, response_code, data[])
{
    // In this callback "index" would normally be called "playerid" ( if you didn't get it already :) )
    new
        buffer[ 128 ];
    if(response_code == 200) //Did the request succeed?
    {
        //Yes!
        format(buffer, sizeof(buffer), "The URL replied: %s", data);
        SendClientMessage(index, 0xFFFFFFFF, buffer);
    }
    else
    {
        //No!
        format(buffer, sizeof(buffer), "The request failed! The response code was: %d", response_code);
        SendClientMessage(index, 0xFFFFFFFF, buffer);
    }
}
Personal tools
Navigation
Toolbox