Skip to content

Instantly share code, notes, and snippets.

@zacck-zz
Created August 8, 2018 13:38
Show Gist options
  • Save zacck-zz/f377c846bf15673d9d351e1b3c788f48 to your computer and use it in GitHub Desktop.
Save zacck-zz/f377c846bf15673d9d351e1b3c788f48 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class scripty : MonoBehaviour {
[Serializable]
public struct Param {
public string key;
public string value;
}
public string api_key = "";
public string url = "";
public Param[] data;
public void CallApi() {
StartCoroutine(ApiCoroutine(data, url));
}
IEnumerator ApiCoroutine(Param[] thedata, string apiUrl) {
WWWForm formData = new WWWForm();
Debug.Log(apiUrl);
// get key from input array
string auth = api_key + ":";
auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
auth = "Basic " + auth;
foreach (var param in thedata)
{
formData.AddField(param.key, param.value);
}
// get url from input array
UnityWebRequest www = UnityWebRequest.Post(apiUrl, formData);
www.SetRequestHeader("AUTHORIZATION", auth);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www);
}
else {
Debug.Log("Form upload complete!");
}
// Debug.Log("Quote created");
yield return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment