ISource2ModHTTP

Interface for HTTP requests.

Requests go out through Steam's own HTTP client, which the game server already has, so there is no curl to link and nothing to pump. Steam only hands that client over once the API has activated, which happens after level startup -- requests made before then are queued and sent when it does, so a plugin does not have to care whether it is early.

Responses come back on the main thread.

Issues HTTP requests through Steam's HTTP client.

HTTP_GET("https://example.com/api", [](const Source2ModHTTPResponse& r)
{
    if (!r.m_bSuccess || r.m_nStatusCode != 200)
        return;

    // r.m_sBody -- parse with whatever your plugin already uses.
});

Interface id ISource2ModHTTP002 (SOURCE2MOD_HTTP_INTERFACE) · Global g_pSource2ModHTTP · Header source2mod/ISource2ModHTTP.h

Types

Source2ModHTTPCallback

using Source2ModHTTPCallback = Source2ModCallback<void(const Source2ModHTTPResponse& response)>;

Called with the response on the main thread: a function, an object and a method, or a lambda (Source2ModCallback) -- whose it is, the core reads off it.

Methods

IsAvailable

virtual bool IsAvailable() const = 0;

Whether Steam's HTTP client is available yet.

Requests made while this is false are queued, not dropped, so checking is only worth it to avoid building a body you cannot send.

PendingCount

virtual int PendingCount() const = 0;

Number of requests waiting for Steam's HTTP client.

Request

virtual void Request(ESource2ModHTTPMethod method, const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;

Issues a request.

The plugin the callback's code lies in owns the request.

Cancelled, callback and all, if the owning plugin unloads first -- the callback holds code inside that plugin's library.
ParameterTypeDefaultDescription
methodESource2ModHTTPMethodHTTP method
pszUrlconst char*Absolute URL
pszBodyconst char*Request body, or nullptr for methods that take none
callbackSource2ModHTTPCallbackCalled once the request completes; may be null
pHeadersconst std::vector<Source2ModHTTPHeader>*nullptrExtra request headers, or nullptr

Get

virtual void Get(const char* pszUrl, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;
ParameterTypeDefault
pszUrlconst char*
callbackSource2ModHTTPCallback
pHeadersconst std::vector<Source2ModHTTPHeader>*nullptr

Post

virtual void Post(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;
ParameterTypeDefault
pszUrlconst char*
pszBodyconst char*
callbackSource2ModHTTPCallback
pHeadersconst std::vector<Source2ModHTTPHeader>*nullptr

Put

virtual void Put(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;
ParameterTypeDefault
pszUrlconst char*
pszBodyconst char*
callbackSource2ModHTTPCallback
pHeadersconst std::vector<Source2ModHTTPHeader>*nullptr

Patch

virtual void Patch(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;
ParameterTypeDefault
pszUrlconst char*
pszBodyconst char*
callbackSource2ModHTTPCallback
pHeadersconst std::vector<Source2ModHTTPHeader>*nullptr

Delete

virtual void Delete(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;
ParameterTypeDefault
pszUrlconst char*
pszBodyconst char*
callbackSource2ModHTTPCallback
pHeadersconst std::vector<Source2ModHTTPHeader>*nullptr

Source2ModHTTPHeader

One request or response header.

Fields

FieldType
m_sNamestd::string
m_sValuestd::string

Constructors

Source2ModHTTPHeader() = default;
Source2ModHTTPHeader(std::string name, std::string value);

Source2ModHTTPResponse

What a completed request came back with.

m_bSuccess is about the transport, not the status code -- a 404 that arrived is a success with m_nStatusCode == 404. A request that never completed has m_bSuccess false and m_nStatusCode 0.

Fields

FieldTypeDescription
m_bSuccessbool
m_nStatusCodeint
m_sBodystd::string
m_pJsonconst ISource2ModJSONValue*The body parsed as JSON, when it was JSON. Owned by Source2Mod and valid only for the duration of the callback -- copy anything you keep. Null when the body was empty or did not parse.

Enums

ESource2ModHTTPMethod

enum class ESource2ModHTTPMethod : int

HTTP method of a request.

NameValue
Get0
Post
Put
Patch
Delete
Head

Macros

MacroExpands to
HTTP_REQUEST(method, pszUrl, pszBody, cb, ...)g_pSource2ModHTTP->Request(method, pszUrl, pszBody, cb, ##__VA_ARGS__)
HTTP_GET(pszUrl, cb, ...)g_pSource2ModHTTP->Get(pszUrl, cb, ##__VA_ARGS__)
HTTP_POST(pszUrl, pszBody, cb, ...)g_pSource2ModHTTP->Post(pszUrl, pszBody, cb, ##__VA_ARGS__)
HTTP_PUT(pszUrl, pszBody, cb, ...)g_pSource2ModHTTP->Put(pszUrl, pszBody, cb, ##__VA_ARGS__)
HTTP_PATCH(pszUrl, pszBody, cb, ...)g_pSource2ModHTTP->Patch(pszUrl, pszBody, cb, ##__VA_ARGS__)
HTTP_DELETE(pszUrl, pszBody, cb, ...)g_pSource2ModHTTP->Delete(pszUrl, pszBody, cb, ##__VA_ARGS__)

On this page