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.
| Parameter | Type | Default | Description |
|---|---|---|---|
method | ESource2ModHTTPMethod | HTTP method | |
pszUrl | const char* | Absolute URL | |
pszBody | const char* | Request body, or nullptr for methods that take none | |
callback | Source2ModHTTPCallback | Called once the request completes; may be null | |
pHeaders | const std::vector<Source2ModHTTPHeader>* | nullptr | Extra request headers, or nullptr |
Get
virtual void Get(const char* pszUrl, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;| Parameter | Type | Default |
|---|---|---|
pszUrl | const char* | |
callback | Source2ModHTTPCallback | |
pHeaders | const std::vector<Source2ModHTTPHeader>* | nullptr |
Post
virtual void Post(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;| Parameter | Type | Default |
|---|---|---|
pszUrl | const char* | |
pszBody | const char* | |
callback | Source2ModHTTPCallback | |
pHeaders | const std::vector<Source2ModHTTPHeader>* | nullptr |
Put
virtual void Put(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;| Parameter | Type | Default |
|---|---|---|
pszUrl | const char* | |
pszBody | const char* | |
callback | Source2ModHTTPCallback | |
pHeaders | const std::vector<Source2ModHTTPHeader>* | nullptr |
Patch
virtual void Patch(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;| Parameter | Type | Default |
|---|---|---|
pszUrl | const char* | |
pszBody | const char* | |
callback | Source2ModHTTPCallback | |
pHeaders | const std::vector<Source2ModHTTPHeader>* | nullptr |
Delete
virtual void Delete(const char* pszUrl, const char* pszBody, Source2ModHTTPCallback callback, const std::vector<Source2ModHTTPHeader>* pHeaders = nullptr) = 0;| Parameter | Type | Default |
|---|---|---|
pszUrl | const char* | |
pszBody | const char* | |
callback | Source2ModHTTPCallback | |
pHeaders | const std::vector<Source2ModHTTPHeader>* | nullptr |
Source2ModHTTPHeader
One request or response header.
Fields
| Field | Type |
|---|---|
m_sName | std::string |
m_sValue | std::string |
Constructors
Source2ModHTTPHeader() = default;
Source2ModHTTPHeader(std::string name, std::string value);Source2ModHTTPResponse
What a completed request came back with.
Fields
| Field | Type | Description |
|---|---|---|
m_bSuccess | bool | |
m_nStatusCode | int | |
m_sBody | std::string | |
m_pJson | const 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 : intHTTP method of a request.
| Name | Value |
|---|---|
Get | 0 |
Post | |
Put | |
Patch | |
Delete | |
Head |
Macros
| Macro | Expands 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__) |