Skip to content

Instantly share code, notes, and snippets.

@amimaro
Last active February 5, 2020 08:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amimaro/d3f618e906114a13e55ae23aa9c81a9d to your computer and use it in GitHub Desktop.
Save amimaro/d3f618e906114a13e55ae23aa9c81a9d to your computer and use it in GitHub Desktop.
Unity Http Request Example
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class UnityRequest : MonoBehaviour
{
bool display = false;
private GUIStyle guiStyle = new GUIStyle ();
public string uri = "http://localhost:8080";
void Start ()
{
}
void Update ()
{
}
public void Get ()
{
StartCoroutine (SendGet ());
}
public void Post (string data)
{
StartCoroutine (SendPost (data));
}
IEnumerator SendGet ()
{
UnityWebRequest www = UnityWebRequest.GetTexture (uri);
yield return www.Send ();
if (www.isError) {
Debug.Log (www.error);
} else {
string result = ((DownloadHandler)www.downloadHandler).text;
}
}
IEnumerator SendPost (string pin)
{
List<IMultipartFormSection> formData = new List<IMultipartFormSection> ();
formData.Add (new MultipartFormDataSection ("pin=" + pin));
UnityWebRequest www = UnityWebRequest.Post (uri, formData);
yield return www.Send ();
if (www.isError) {
Debug.Log (www.error);
} else {
Debug.Log ("Form upload complete!");
string result = ((DownloadHandler)www.downloadHandler).text;
}
}
}
@LeonardoFassini
Copy link

Hello!
Im learning how to use it on unity, and I'm having some questions.
If I want to add more fields, like:

Name: XXXX
Date of Birth: YYYYY
BlaBlaBla: FFFFFF

Do i need to put inside one and only one MultipartFormDataSection, or i can put in more than one, so it gets a little bit more "readable".
Like:

formData.Add (new MultipartFormDataSection ("Name=" + XXXX));
formData.Add (new MultipartFormDataSection ("Date of Birth=" + YYYYY));
etc.

Thanks for your attention!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment