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);
}
}