Can't send POST requests from Unity, but it works on Postman

I’ve searched the problem, but didn’t found any solution, even tough I’ve seen similar questions beign asked.
I’m receiving HTTP/1.1 500 Internal Server Error

In line 26, there is a Debug.log(txt), which is the string created from the JsonUtility.ToJson(). I copied this txt and pasted in Postman and it worked just fine. So I don’t know what I could be doing wrong.

Thanks in advance.

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class TEST : MonoBehaviour
{
    [Serializable]
    public class Player
    {
        public string name;
        public int score;

        public Player(string name, int score)
        {
            this.name = name;
            this.score = score;
        }
    }

    void Start()
    {
        Player p = new Player("MMM", 200);
        string txt = JsonUtility.ToJson(p);

        Debug.Log(txt);

        StartCoroutine(Post("valid url, same as used in postman", txt));
    }

    IEnumerator Post(string uri, string postData)
    {
        using (UnityWebRequest www = UnityWebRequest.Post(uri, postData))
        {
            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Form upload complete!");
            }
        }
    }
}

Use Charles Proxy to compare a working request vs non-working https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity Save each session as a separate .chls file. If you need help analyzing the capture, send them to me in a private message here and I will take a look. I should add, this only works when your webserver is not running on your local system (localhost)

1 Like

After using Charles Proxy I could find the problem, then I found a solution searching online. Here’s the updated code working fine (changes were only made to the Post IEnumerator):

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class TEST : MonoBehaviour
{
    [Serializable]
    public class Player
    {
        public string name;
        public int score;
        public Player(string name, int score)
        {
            this.name = name;
            this.score = score;
        }
    }
    void Start()
    {
        Player p = new Player("MMM", 200);
        string txt = JsonUtility.ToJson(p);
        Debug.Log(txt);
        StartCoroutine(Post("valid url, same as used in postman", txt));
    }
    IEnumerator Post(string uri, string postData)
    {
        using (UnityWebRequest www = new UnityWebRequest(uri, "POST"))
        {
            www.SetRequestHeader("Content-Type", "application/json");
            byte[] rawData = Encoding.UTF8.GetBytes(postData);
            www.uploadHandler = new UploadHandlerRaw(rawData);

            yield return www.SendWebRequest();
            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Post data upload complete!");
            }
        }
    }
}
1 Like