Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
/ sm-httpreq Public archive

Provides the ability to make simple HTTP requests via the socket extension in SourcePawn.

License

Notifications You must be signed in to change notification settings

clugg/sm-httpreq

Repository files navigation

sm-httpreq

I would not recommend this for use in production. It does not support HTTPS and is therefore not secure. I consider this more of a learning/informative project. If you are looking for a more fully featured HTTP requests library, I would recommend SteamWorks.

Provides the ability to make simple HTTP requests via the Socket extension in SourcePawn.

Crafts and sends basic HTTP requests, with support for setting headers and parameters. Parameters are first crafted into a query string (no URL escaping is performed), then either appended to the URI when using GET, or placed in the request body (with Content-Type: application/x-www-form-urlencoded and Content-Length headers automatically added) otherwise. This library does not fully support HTTP/1.1 specifications.

Thanks to f0oster for helping with some concepts.

Requirements

  • SourceMod 1.7 or later
  • Socket 3.0.1 - a transitional syntax version of the include can be found here

Example Usage

#include <httpreq>

public void OnPluginStart()
{
    HTTPRequest req = new HTTPRequest("GET", "http://example.com/api", OnRequestComplete);
    req.debug = true;
    req.headers.SetString("User-Agent", "sm-httpreq by clugg");
    req.params.SetString("test", "param");
    req.SendRequest();
}

void OnRequestComplete(HTTPRequest req, bool success, int statusCode, FancyStringMap headers, const char[] body, int errorType, int errorNum, any data)
{
    if (success) {
        PrintToServer("finished request with status code %d", statusCode);
        PrintToServer("headers:");

        StringMapSnapshot snap = headers.Snapshot();
        for (int i = 0, keyLength = 0, valueLength = 0; i < snap.Length; ++i) {
            // get header name
            keyLength = snap.KeyBufferSize(i);
            char[] key = new char[keyLength];
            snap.GetKey(i, key, keyLength);

            if (headers.IsBufferKey(key)) {
                continue;
            }

            // get header value
            valueLength = headers.StringBufferSize(key);
            char[] value = new char[valueLength];
            headers.GetString(key, value, valueLength);

            // output header
            PrintToServer("%s: %s", key, value);
        }
        delete snap;

        PrintToServer("response: %s", body);
    } else {
        PrintToServer("request failed with error type %d, error num %d", errorType, errorNum);
    }

    req.Cleanup();
    delete req;
    delete headers;
}

About

Provides the ability to make simple HTTP requests via the socket extension in SourcePawn.

Resources

License

Stars

Watchers

Forks

Packages

No packages published