Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by gabrielmdemelo, May 3, 2022.

  1. gabrielmdemelo

    gabrielmdemelo

    Joined:
    Jan 22, 2021
    Posts:
    3
    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.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class TEST : MonoBehaviour
    7. {
    8.     [Serializable]
    9.     public class Player
    10.     {
    11.         public string name;
    12.         public int score;
    13.  
    14.         public Player(string name, int score)
    15.         {
    16.             this.name = name;
    17.             this.score = score;
    18.         }
    19.     }
    20.  
    21.     void Start()
    22.     {
    23.         Player p = new Player("MMM", 200);
    24.         string txt = JsonUtility.ToJson(p);
    25.  
    26.         Debug.Log(txt);
    27.  
    28.         StartCoroutine(Post("valid url, same as used in postman", txt));
    29.     }
    30.  
    31.     IEnumerator Post(string uri, string postData)
    32.     {
    33.         using (UnityWebRequest www = UnityWebRequest.Post(uri, postData))
    34.         {
    35.             yield return www.SendWebRequest();
    36.  
    37.             if (www.result != UnityWebRequest.Result.Success)
    38.             {
    39.                 Debug.Log(www.error);
    40.             }
    41.             else
    42.             {
    43.                 Debug.Log("Form upload complete!");
    44.             }
    45.         }
    46.     }
    47. }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Last edited: May 4, 2022
    gabrielmdemelo likes this.
  3. gabrielmdemelo

    gabrielmdemelo

    Joined:
    Jan 22, 2021
    Posts:
    3
    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):

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. public class TEST : MonoBehaviour
    6. {
    7.     [Serializable]
    8.     public class Player
    9.     {
    10.         public string name;
    11.         public int score;
    12.         public Player(string name, int score)
    13.         {
    14.             this.name = name;
    15.             this.score = score;
    16.         }
    17.     }
    18.     void Start()
    19.     {
    20.         Player p = new Player("MMM", 200);
    21.         string txt = JsonUtility.ToJson(p);
    22.         Debug.Log(txt);
    23.         StartCoroutine(Post("valid url, same as used in postman", txt));
    24.     }
    25.     IEnumerator Post(string uri, string postData)
    26.     {
    27.         using (UnityWebRequest www = new UnityWebRequest(uri, "POST"))
    28.         {
    29.             www.SetRequestHeader("Content-Type", "application/json");
    30.             byte[] rawData = Encoding.UTF8.GetBytes(postData);
    31.             www.uploadHandler = new UploadHandlerRaw(rawData);
    32.  
    33.             yield return www.SendWebRequest();
    34.             if (www.result != UnityWebRequest.Result.Success)
    35.             {
    36.                 Debug.Log(www.error);
    37.             }
    38.             else
    39.             {
    40.                 Debug.Log("Post data upload complete!");
    41.             }
    42.         }
    43.     }
    44. }
     
    JeffDUnity3D likes this.