The Http system has a quick and easy API for making http requests within Unity.
The Http instance will run the WebRequest coroutines for you so you dont have to create it per request.
- Singleton
- Fluent API for configuration
- Success, error and network error events
- Super headers
- Unity 2018.3+
- .NET 4.5
- C# 7
Add it as a package using Unity Package Manager or via submodule:
git submodule add git@github.com:dubit/unity-http.git Assets/Duck/Http
- Use gitflow
- Create a release branch for the release
- On that branch, bump version number in package json file, any other business (docs/readme updates)
- Merge to master via pull request and tag the merge commit on master.
- Merge back to development.#
If you are using an AssemblyDefinition then reference the Http Assembly.
Import the namespace using DUCK.Http;
var request = Http.Get("http://mywebapi.com/")
.SetHeader("Authorization", "username:password")
.OnSuccess(response => Debug.Log(response.Text))
.OnError(response => Debug.Log(response.StatusCode))
.OnDownloadProgress(progress => Debug.Log(progress))
.Send();
All these methods return a new HttpRequest.
Http.Get(string uri)
Http.GetTexture(string uri)
Http.Post(string uri, string postData)
Http.Post(string uri, WWWForm formData)
Http.Post(string uri, Dictionary<string, string> formData))
Http.Post(string uri, List<IMultipartFormSection> multipartForm)
Http.Post(string uri, byte[] bytes, string contentType)
Http.PostJson(string uri, string json)
Http.PostJson<T>(string uri, T payload)
Http.Put(string uri, byte[] bodyData)
Http.Put(string uri, string bodyData)
Http.Delete(string uri)
Http.Head(string uri)
All these methods return the HttpRequest instance.
SetHeader(string key, string value)
SetHeaders(IEnumerable<KeyValuePair<string, string>> headers)
RemoveHeader(string key)
RemoveSuperHeaders()
OnSuccess(Action<HttpResonse> response)
OnError(Action<HttpResonse> response)
OnNetworkError(Action<HttpResonse> response)
OnUploadProgress(Action<float> progress)
OnDownloadProgress(Action<float> progress)
SetRedirectLimit(int redirectLimit)
SetTimeout(int duration)
Redirect limit subject to Unity's documentation.
Progress events will invoke each time the progress value has increased, they are subject to Unity's documentation.
HttpRequest Send()
void Abort()
The callbacks for OnSuccess
, OnError
and OnNetworkError
all return you a HttpResponse
.
This has the following properties:
string Url
bool IsSuccessful
bool IsHttpError
bool IsNetworkError
long StatusCode
ResponseType ResponseType
byte[] Bytes
string Text
string Error
Texture Texture
Dictionary<string, string> ResponseHeaders
Super Headers are a type of Header that you can set once to automatically attach to every Request you’re sending.
They are Headers that apply to all requests without having to manually include them in each HttpRequest SetHeader call.
Http.SetSuperHeader(string key, string value)
Http.RemoveSuperHeader(string key)
returnsbool
Http.GetSuperHeaders()
returnsDictionary<string, string>
In this given example, the response.Text
from http://mywebapi.com/user.json
is:
{
"id": 92,
"username": "jason"
}
Create a serializable class that maps the data from the json response to fields
[Serializable]
public class User
{
[SerializeField]
public int id;
[SerializeField]
public string username;
}
We can listen for the event OnSuccess
with our handler method HandleSuccess
var request = Http.Get("http://mywebapi.com/user.json")
.OnSuccess(HandleSuccess)
.OnError(response => Debug.Log(response.StatusCode))
.Send();
Parse the response.Text
to the serialized class User
that we declared earlier by using Unity's built in JSONUtility
private void HandleSuccess(HttpResponse response)
{
var user = JsonUtility.FromJson<User>(response.Text);
Debug.Log(user.username);
}