Search code examples
c#jsonunity-game-engineuber-api

Make call to Uber API in JSON format from within Unity


I am using the Uber API inside of Unity and I am able to login and then authenticate to get the Token, but I have run into a roadblock when calling the actual API.

I believe my issue is that I need to be making the call in JSON format, but I don't know how to do that. I'm new to HTTP and API's in general. Here is my code:

    private IEnumerator TestRequest(){
    Debug.Log(sToken);
    WWWForm form = new WWWForm();
    //WWW www = new WWW();
    form.headers["Content-Type"] = "application.json";
    form.headers["Authorization"] = "Bearer " +sToken;
    form.AddField( "fare_id", "abcd");
    form.AddField("product_id", "a1111c8c-c720-46c3-8534-2fcdd730040d");
    form.AddField("start_latitude", "37.761492");
    form.AddField("start_longitude", "-122.42394");
    form.AddField("end_latitude", "37.775393");
    form.AddField("end_longitude", "-122.417546");

    yield return null;

    using(UnityWebRequest uweb = UnityWebRequest.Post("https://sandbox-
    api.uber.com/v1.2/requests", form)){
        yield return uweb.Send();
        if(uweb.isError) Debug.Log(uweb.error);
        else Debug.Log(uweb.downloadHandler.text);
        //GetVals(uweb.downloadHandler.text);
    }
}

This works for me in other areas, but not in this one and I think it has something to do with the Content Type being JSON, but I can't figure out how to send it in the right format. Apologies that I can't be more specific, I'm only just getting my head around this stuff.

Any help would be greatly appreciated!


Solution

  • Like others mentioned, application.json should be application/json.

    This is not the only problem. Since it is a json, you don't need to use WWWForm class. Create a class to hold the Json data then create new instance of it. Convert the instance to json and pass it to the second parameter of the UnityWebRequest Post function.

    UnityWebRequest:

    For UnityWebRequest, use the UnityWebRequest Post(string uri, string postData); overload which let's you pass the url and the json data. You then use SetRequestHeader to set the headers.

    [Serializable]
    public class UberJson
    {
        public string fare_id;
        public string product_id;
        public double start_latitude;
        public double start_longitude;
        public double end_latitude;
        public double end_longitude;
    }
    
    void Start()
    {
        postJson();
    }
    
    string createUberJson()
    {
        UberJson uberJson = new UberJson();
    
        uberJson.fare_id = "abcd";
        uberJson.product_id = "a1111c8c-c720-46c3-8534-2fcdd730040d";
        uberJson.start_latitude = 37.761492f;
        uberJson.start_longitude = -122.42394f;
        uberJson.end_latitude = 37.775393f;
        uberJson.end_longitude = -122.417546f;
    
        //Convert to Json
        return JsonUtility.ToJson(uberJson);
    }
    
    void postJson()
    {
        string URL = "https://sandbox-api.uber.com/v1.2/requests";
    
        //string json = "{ \"fare_id\": \"abcd\", \"product_id\": \"a1111c8c-c720-46c3-8534-2fcdd730040d\", \"start_latitude\": 37.761492, \"start_longitude\": -122.423941, \"end_latitude\": 37.775393, \"end_longitude\": -122.417546 }";
    
        string json = createUberJson();
    
        string sToken = "";
    
        //Set the Headers
        UnityWebRequest uwrq = UnityWebRequest.Post(URL, json);
        uwrq.SetRequestHeader("Content-Type", "application/json");
        uwrq.SetRequestHeader("Authorization", "Bearer " + sToken);
    
        StartCoroutine(WaitForRequest(uwrq));
    }
    
    IEnumerator WaitForRequest(UnityWebRequest uwrq)
    {
        //Make the request
        yield return uwrq.Send();
        if (String.IsNullOrEmpty(null))
        {
            Debug.Log(uwrq.downloadHandler.text);
        }
        else
        {
            Debug.Log("Error while rececing: " + uwrq.error);
        }
    }
    

    If UnityWebRequest did not work, use WWW. There have been reports of bugs with UnityWebRequest but I have not personally encountered one.

    WWW:

    For WWW, use the public WWW(string url, byte[] postData, Dictionary<string, string> headers); constructor overload which takes in the url, the data and the headers in one function call.

    [Serializable]
    public class UberJson
    {
        public string fare_id;
        public string product_id;
        public double start_latitude;
        public double start_longitude;
        public double end_latitude;
        public double end_longitude;
    }
    
    void Start()
    {
        postJson();
    }
    
    string createUberJson()
    {
        UberJson uberJson = new UberJson();
    
        uberJson.fare_id = "abcd";
        uberJson.product_id = "a1111c8c-c720-46c3-8534-2fcdd730040d";
        uberJson.start_latitude = 37.761492f;
        uberJson.start_longitude = -122.42394f;
        uberJson.end_latitude = 37.775393f;
        uberJson.end_longitude = -122.417546f;
    
        //Convert to Json
        return JsonUtility.ToJson(uberJson);
    }
    
    
    void postJson()
    {
        string URL = "https://sandbox-api.uber.com/v1.2/requests";
    
        //string json = "{ \"fare_id\": \"abcd\", \"product_id\": \"a1111c8c-c720-46c3-8534-2fcdd730040d\", \"start_latitude\": 37.761492, \"start_longitude\": -122.423941, \"end_latitude\": 37.775393, \"end_longitude\": -122.417546 }";
    
        string json = createUberJson();
    
        string sToken = "";
    
        //Set the Headers
        Dictionary<string, string> headers = new Dictionary<string, string>();
        headers.Add("Content-Type", "application/json");
        headers.Add("Authorization", "Bearer " + sToken);
        //headers.Add("Content-Length", json.Length.ToString());
    
        //Encode the JSON string into a bytes
        byte[] postData = System.Text.Encoding.UTF8.GetBytes(json);
    
        WWW www = new WWW(URL, postData, headers);
        StartCoroutine(WaitForRequest(www));
    }
    
    IEnumerator WaitForRequest(WWW www)
    {
        yield return www;
        if (String.IsNullOrEmpty(null))
        {
            Debug.Log(www.text);
        }
        else
        {
            Debug.Log("Error while rececing: " + www.error);
        }
    }