Sending a form to an HTTP server (POST) Using IMultipartFormSection

Unity3D 2018. 10. 19. 16:54
반응형

Sending a form to an HTTP server (POST) Using IMultipartFormSection

서버에서 데이터를 못받는 (AddPerson은 안되고 Post는 되는 이유?)



UnityWebRequest.PostUnityWebRequest.Get and other UnityWebRequest functions that creates new instance of UnityWebRequest, will automatically have DownloadHandlerBuffer attached to it.

Now, if you create new instance of UnityWebRequest with the UnityWebRequest constructor, DownloadHandler is not attached to that new instance. You have to manually do that with DownloadHandlerBuffer.


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


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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
 
//[System.Serializable]
public class add_person
{
    public string firstName;
    public string lastName;
    public int age;
 
    public add_person(string firstName, string lastName, int age)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
}
 
public class Test : MonoBehaviour {
    
    public object[] Encording { get; private set; }
    private List<PersonData> personDataList;
 
    // Use this for initialization
    void Start () {
        
    }
 
    IEnumerator AddPerson(PersonData personData)
    {
        var add_person = new add_person(personData.firstName, personData.lastName, personData.age);
 
        var json = JsonConvert.SerializeObject(add_person);
 
        Debug.LogFormat("{0}", json);
 
        List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
        formData.Add(new MultipartFormDataSection("data", json));
 
        UnityWebRequest www = UnityWebRequest.Post("localhost:3000/addPerson", formData);
        yield return www.SendWebRequest();
 
        if (www.isNetworkError || www.isHttpError)
        {
            Debug.LogFormat("{0}", www.error);
        }
        else {
            Debug.LogFormat("{0}", www.downloadHandler.text);
        }
 
        
    }
 
    IEnumerator GetPersons()
    {
        using (UnityWebRequest www = UnityWebRequest.Get("localhost:3000/personList"))
        {
            yield return www.SendWebRequest();
 
            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else {
                Debug.Log(www.downloadHandler.text);
 
                byte[] results = www.downloadHandler.data;
 
                var json = System.Text.Encoding.Default.GetString(results);
 
                Debug.LogFormat("{0} {1}", results.Length, json);
 
                this.personDataList = JsonConvert.DeserializeObject<List<PersonData>>(json);
 
                foreach (var data in this.personDataList)
                {
                    Debug.LogFormat("{0}, {1}, {2}, {3}", data.id, data.firstName, data.lastName, data.age);
                }
            }
        }
    }
 
    // server side post data is null ?
    //https://stackoverflow.com/questions/43292700/unitywebrequest-downloadhandler-returning-null-while-getting-response-from-node?rq=1
    IEnumerator Post(string uri , string b)
    {
 
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(b);
 
        using (UnityWebRequest www = new UnityWebRequest(uri, UnityWebRequest.kHttpVerbPOST))
        {
            UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
            DownloadHandlerBuffer dH = new DownloadHandlerBuffer();
 
            www.uploadHandler = uH;
            www.downloadHandler = dH;
            www.SetRequestHeader("Content-Type""application/json");
            yield return www.SendWebRequest();
 
            if (www.isHttpError || www.isNetworkError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log(www.ToString());
                Debug.Log(www.downloadHandler.text);
            }
        }
    }
 
 
    private void OnGUI()
    {
        if (GUILayout.Button("GET", GUILayout.Width(100), GUILayout.Height(60)))
        {
            StartCoroutine(GetPersons());
        }
 
        if (GUILayout.Button("POST", GUILayout.Width(100), GUILayout.Height(60)))
        {
            var personData = new PersonData("aaaaaaa""bbbbbb"1234);
            //StartCoroutine(AddPerson(personData));
            var json = JsonConvert.SerializeObject(personData);
            StartCoroutine(this.Post("localhost:3000/addPerson", json));
        }
    }
}
 
cs


반응형

'Unity3D' 카테고리의 다른 글

IAP 테스트 (영수증검증)  (0) 2018.10.23
Socket.IO-Client-Unity3D  (0) 2018.10.19
How to Normalize a Vector  (0) 2018.09.06
피타고라스 정리  (0) 2018.09.06
벡터란?  (0) 2018.09.06
: