Skip to main content
Files:
  • Assets/_Molca/_Core/Networking/Http/HttpClient.cs
  • Assets/_Molca/_Core/Networking/Http/HttpRequestAsset.cs

When to use

  • HttpRequestAsset (recommended) — for data-driven endpoints where URL, method, headers, and body templates live in a ScriptableObject. Best for stable API calls configured once in the Inspector.
  • HttpRequestBuilder (programmatic) — for dynamic endpoints or when URL/body must be built at runtime. Uses a fluent builder API.
  • Both route through the same HttpClient subsystem with queuing, concurrency control, and connection error events.

HttpClient

RuntimeSubsystem — async HTTP with request queuing, auth header management, and event hooks.
MethodSignatureDescription
SendAsyncstatic async Awaitable<HttpResponse> SendAsync(HttpRequest request)Send request and await response
Sendstatic void Send(HttpRequest req, Action<HttpResponse> onSuccess, Action<string> onError, Action<float> onProgress)Callback-style send
CreateRequeststatic HttpRequestBuilder CreateRequest()Start a fluent builder chain
AddDefaultHeaderstatic void AddDefaultHeader(string key, string value)Add header to all future requests
RemoveDefaultHeaderstatic void RemoveDefaultHeader(string key)Remove a default header
CancelAllRequestsstatic void CancelAllRequests()Abort all active requests
Events: OnRequestStarted, OnRequestCompleted, OnRequestFailed, OnConnectionError (static event Action). Properties: BaseUrl, MaxConcurrentRequests, ActiveRequestCount, RequestHistory.

HttpRequestAsset

ScriptableObject describing URL, method, headers, and body templates. Create via Molca → Networking → HTTP Request.
MethodSignatureDescription
SendAsyncasync Awaitable<HttpResponse> SendAsync()Send this asset’s configured request
Sendvoid Send(Action<HttpResponse> onSuccess, Action<string> onError, Action<float> onProgress)Callback-style send
CreateBuilderHttpRequestBuilder CreateBuilder()Clone into a builder for runtime modifications
Validatebool Validate(out string[] errors)Check configuration before sending

Code

From a HttpRequestAsset (recommended when the request is mostly data-driven):
using Molca.Networking.Http;
using UnityEngine;

public class ProfileClient : MonoBehaviour
{
    [SerializeField] private HttpRequestAsset fetchProfile;

    private async void Start()
    {
        await RuntimeManager.WaitForInitialization();
        var response = await fetchProfile.SendAsync();
        if (response.IsSuccess)
            Debug.Log(response.Text);
    }
}
Programmatic requests using the fluent builder:
using Molca.Networking.Http;
using UnityEngine;

public class DynamicApiClient : MonoBehaviour
{
    private async void FetchUser(int userId)
    {
        var response = await HttpClient.CreateRequest()
            .Method(HttpMethod.GET)
            .Url($"/api/users/{userId}")
            .Header("Accept", "application/json")
            .Timeout(10)
            .SendAsync();

        if (response.isSuccess)
            Debug.Log(response.Text);
        else
            Debug.LogError(response.errorMessage);
    }
}

Troubleshooting

  • Request fails silently: subscribe to HttpClient.OnRequestFailed or OnConnectionError for diagnostics. The request may have been queued but not started if MaxConcurrentRequests is exceeded.
  • Auth header missing on requests: call HttpClient.AddDefaultHeader("Authorization", "Bearer " + token) after login. Default headers are applied to all future requests automatically.
  • Wrong base URL: BaseUrl comes from HttpModule on Global Settings. If the module is missing, URLs must be absolute (set useFullUrl = true or use FullUrl() on the builder).
  • Request validation fails: HttpRequestAsset.Validate(out errors) will return false if the URL is empty or malformed. Check the errors array for specifics.

Unity Editor

Select an HttpRequestAsset under Scriptable Objects / Http Request (or your project folder).

HttpRequestAsset ScriptableObject in Unity Inspector