Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity send post request json

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Data;
using UnityEngine.Networking;

[System.Serializable]
public class User
{
    public string Name;
    public string ID;
    
}

[System.Serializable]
public class Users
{
    public List<User> users = new List<User>();
}

public class Test : MonoBehaviour
{
    void Start()
    {
        //Test용 User create
        User user1 = new User
        {
            Name = "Kim",
            ID = "qq"
        };

        User user2 = new User
        {
            Name = "Lee",
            ID = "tt"
        };

        Users user_arr = new Users();

        //Add user
        user_arr.users.Add(user1);
        user_arr.users.Add(user2);

        //Convert JsonString
        string json = JsonUtility.ToJson(user_arr);
        
        //request Post
        StartCoroutine(Upload("http://URL", json));
    }

    IEnumerator Upload(string URL, string json)
    {
        using (UnityWebRequest request = UnityWebRequest.Post(URL, json))
        {
            byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
            request.uploadHandler = new UploadHandlerRaw(jsonToSend);
            request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
            request.SetRequestHeader("Content-Type", "application/json");

            yield return request.SendWebRequest();

            if (request.isNetworkError || request.isHttpError)
            {
                Debug.Log(request.error);
            }
            else
            {
                Debug.Log(request.downloadHandler.text);
            }

        }
    }
 }
Comment

PREVIOUS NEXT
Code Example
Csharp :: xamarin picker 
Csharp :: c# array of class 
Csharp :: unity keep screen always on 
Csharp :: Dyanmically create datatable in c# 
Csharp :: c# excel workbook 
Csharp :: c# remove all whitespaces from string 
Csharp :: c# check if char is string 
Csharp :: convert-integer-to-binary-in-c-sharp 
Csharp :: text read C# 
Csharp :: C# setting property values through reflection with attributes 
Csharp :: c# swap name in string 
Csharp :: upload file using httpwebrequest c# 
Csharp :: unity toint 
Csharp :: dotween sequence 
Csharp :: how to get file type from base64 in c# 
Csharp :: unity yield return 
Csharp :: how to get the size an array in unity 
Csharp :: c# directory file 
Csharp :: c# funtion 
Csharp :: scale between tow ranges c# 
Csharp :: how to find player gameobject in unity 
Csharp :: carousel asp.net mvc beginner 
Csharp :: c# normalize value 
Csharp :: unity get prefabs from folder 
Csharp :: how to display array in string in c# 
Csharp :: unity agent look at 
Csharp :: C# max rand 
Csharp :: get file name from stream c# 
Csharp :: .net 4.5 use tls 1.2 
Csharp :: c# mvc get current directory 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =