Test Case: REST API mhma

시작하기

간단히 REST API 호출을 테스트하기 위한 가이드 문서입니다.
PUT, GET, POST 호출과 함께 JWT 토큰을 전송하는 내용으로 구성되어 있습니다.

사전 요구 사항

JSON .NET 설치

에셋스토어에서 JSON.NET을 설치합니다. 이는 Nested Object를 Json String으로 변환하기 위해 필요합니다. JSON.NET 에셋스토어 링크

샘플 코드

PUT, GET, POST 구현부

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;

class RestApi
{
    public RestApi() { }

    public IEnumerator PutWithToken(string url, string token, dynamic request /* , System.Action<object> callback */)
    {
        dynamic paramsJson = JsonConvert.SerializeObject(request);
        byte[] myData = System.Text.Encoding.UTF8.GetBytes(paramsJson);
        var www = new UnityWebRequest (url, UnityWebRequest.kHttpVerbPUT);
        www.uploadHandler = (UploadHandler) new UploadHandlerRaw(myData);
        www.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        www.SetRequestHeader("Content-Type", "application/json");
        www.SetRequestHeader("Authorization", "Bearer " + token);
        yield return www.SendWebRequest();

        if(www.isNetworkError || www.isHttpError) {
            Debug.Log("API Error");
            Debug.Log(www.isNetworkError);
            Debug.Log(www.isHttpError);
            // Error Handling
        } else {
            dynamic response = JsonConvert.DeserializeObject(www.downloadHandler.text);
            if (response["error"] != null)
            {
                Debug.Log("API Error");
                // Error Handling
            }
            else
            {
                Debug.Log("API Success");
                // ResponseClass response = JsonConvert.DeserializeObject<ResponseClass>(response)
            }
            Debug.Log(response);
        }
    }

    public IEnumerator GetWithToken(string url, string token /* , System.Action<object> callback */) {
        UnityWebRequest www = UnityWebRequest.Get(url);
        www.SetRequestHeader("Content-Type", "application/json");
        www.SetRequestHeader("Authorization", "Bearer " + token);
        yield return www.SendWebRequest();

        if(www.isNetworkError || www.isHttpError) {
            Debug.Log(www.error);
        }
        else {
            // Result (Same as Put Example)
            Debug.Log(www.downloadHandler.text);
        }
    }

    public IEnumerator Post(string url, dynamic request /* , System.Action<object> callback */) {
        dynamic paramsJson = JsonConvert.SerializeObject(request);
        byte[] myData = System.Text.Encoding.UTF8.GetBytes(paramsJson);
        var www = new UnityWebRequest (url, UnityWebRequest.kHttpVerbPOST);
        www.uploadHandler = (UploadHandler) new UploadHandlerRaw(myData);
        www.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        www.SetRequestHeader("Content-Type", "application/json");
        yield return www.SendWebRequest();

        if(www.isNetworkError || www.isHttpError) {
            Debug.Log(www.error);
        }
        else {
            // Result (Same as Put Example)
            Debug.Log(www.downloadHandler.text);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

사용 예시

class PutUserParam
{
    public string name = "mamh";
    public int level = 1000;
    public string hid = "h11101";
    public List<Dictionary<string, object>> deck = new List<Dictionary<string, object>>();
}

class PostLoginParam
{
    public string uid = "uid1231231";
}

public class IntroScene : MonoBehaviour
{
    public Start()
    {
        string api_endpoint = "xxxx";
        string token = "xxx"; // JWT Token
        RestApi api = new RestApi();
        PutUserParam putUserParams = new PutUserParam();
        Dictionary<string, object> deck = new Dictionary<string, object>();
        deck.Add("hid", "h11111");
        deck.Add("level", 1000);
        putUserParams.deck.Add(deck);
        StartCoroutine(api.PutWithToken(api_endpoint, token, putUserParams));
        StartCoroutine(api.GetWithToken(api_endpoint, token));
        StartCoroutine(api.Post(api_endpoint, new PostLoginParam()));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Last Updated: 3/13/2019, 1:42:22 PM