28
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnityでRESTでアクセス

Last updated at Posted at 2016-10-10

本Qiitaに関して

RESTでの接続方法に関してのまとめです。

関連Qiita

REST

UnityのC#でRESTでたたきたい場合には、UnityWebRequestを利用する。

GET

   public IEnumerator Get (string url) {
		var request = new UnityWebRequest();
		request.downloadHandler = new DownloadHandlerBuffer();
		request.url = url;
		request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
		request.method = UnityWebRequest.kHttpVerbGET;
		yield return request.Send();

		if(request.isError) {
			Debug.Log(request.error);
		}
		else {
			if (request.responseCode == 200) {
				Debug.Log ("success");
				Debug.Log(request.downloadHandler.text);
			} else {
				Debug.Log ("failed");
			}
		}
	}

POST

    public IEnumerator Post (string url, string jsonStr) {
		var request = new UnityWebRequest();
		request.url = url;
		byte[] body = Encoding.UTF8.GetBytes(jsonStr);
		request.uploadHandler = new UploadHandlerRaw(body);
		request.downloadHandler = new DownloadHandlerBuffer();
		request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
		request.method = UnityWebRequest.kHttpVerbPOST;
		yield return request.Send();

		if(request.isError) {
			Debug.Log(request.error);
		}
		else {
			if (request.responseCode == 200) {
				Debug.Log ("success");
				Debug.Log(request.downloadHandler.text);
			} else {
				Debug.Log ("failed");
			}
		}
	}

DELETE

	public IEnumerator Delete (string url) {
		var request = new UnityWebRequest();
		request.url = url;
		request.downloadHandler = new DownloadHandlerBuffer();
		request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
		request.method = UnityWebRequest.kHttpVerbDELETE;
		yield return request.Send();

		if(request.isError) {
			Debug.Log(request.error);
		}
		else {
			if (request.responseCode == 200) {
				Debug.Log ("success");
				Debug.Log(request.downloadHandler.text);
			} else {
				Debug.Log ("failed");
			}
		}
	}

PUT

	public IEnumerator Put (string url, string jsonStr) {
		var request = new UnityWebRequest();
		request.url = url;
		byte[] body = Encoding.UTF8.GetBytes(jsonStr);
		request.uploadHandler = new UploadHandlerRaw(body);
		request.downloadHandler = new DownloadHandlerBuffer();
		request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");
		request.method = UnityWebRequest.kHttpVerbPUT;
		yield return request.Send();

		if(request.isError) {
			Debug.Log(request.error);
		}
		else {
			if (request.responseCode == 200) {
				Debug.Log ("success");
				Debug.Log(request.downloadHandler.text);
			} else {
				Debug.Log ("failed");
			}
		}
	}
28
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?