我正在尝试向Unity中的restful Web API发出POST请求.
标题是 Content-Type: application/json
原始数据输入的一个示例是,其中data是键,json字符串是值:
{
"data":{
"username":"name",
"email":"email@gmail.com",
"age_range":21,
"gender":"male",
"location":"california"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的脚本:
private static readonly string POSTAddUserURL = "http://db.url.com/api/addUser";
public WWW POST()
{
WWW www;
Hashtable postHeader = new Hashtable();
postHeader.Add("Content-Type", "application/json");
WWWForm form = new WWWForm();
form.AddField("data", jsonStr);
www = new WWW(POSTAddUserURL, form);
StartCoroutine(WaitForRequest(www));
return www;
}
IEnumerator WaitForRequest(WWW data)
{
yield return data; // Wait until the download is done
if (data.error != null)
{
MainUI.ShowDebug("There was an error sending request: " + …Run Code Online (Sandbox Code Playgroud)