Unity 3d call post api with json request

前端 未结 1 549
Happy的楠姐
Happy的楠姐 2020-12-10 08:10

I want to call a login api in unity 3d with two json parameter username and password.

I followed many post available on stackoverflow. But my request parameters are

相关标签:
1条回答
  • 2020-12-10 08:25

    You need to manually set the content header and the body of the message, and convert your form data string to a json string and send how parameter to CallLogin:

    public IEnumerator CallLogin(string url, string logindataJsonString)
    {
        var request = new UnityWebRequest (url, "POST");
        byte[] bodyRaw = Encoding.UTF8.GetBytes(logindataJsonString);
        request.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
        request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");
        yield return request.SendWebRequest();
    
        if (www.error != null)
        {
            Debug.Log("Erro: " + www.error);
        }
        else
        {
            Debug.Log("All OK");
            Debug.Log("Status Code: " + request.responseCode);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题