UnityでRESTでアクセス

# 本Qiitaに関して

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

# 関連Qiita

* [DayDream開発機の設定](http://qiita.com/akira-sasaki/items/582d84b6e3e717aaf661)
* [UnityでのDayDreamデモアプリのBuild](http://qiita.com/akira-sasaki/items/6065e926b76620c12424)
* [DayDream Readyのスマフォ](http://qiita.com/akira-sasaki/items/4d648f9da28ff89973d4)
* [DayDream開発機の作り方](http://qiita.com/akira-sasaki/items/78e86d82dd307c494596)
* [DayDream関連リンク](http://qiita.com/akira-sasaki/items/3f37d7908be802191705)
* [Unity向けのWebSocketライブラリの組み込み](http://qiita.com/akira-sasaki/items/18a72f6b99acaa9601b8)
* [UnityのJsonUtilityでJSON配列を処理する](http://qiita.com/akira-sasaki/items/71c13374698b821c4d73)

# REST

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

https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.html

# GET

```csharp
   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

```csharp
    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
```csharp
	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
```csharp
	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");
			}
		}
	}
```