2017-06-04 11 views
1

イム枠組みとしてイブを使用している私のREST APIにPOSTリクエストをやろうとしているが、しかし、私が処理不能エンティティを言って422エラーが出ます。私のGET要求は完全に正常に動作します。ここに私のイブAppの私のスキーマは次のとおりです。ここで422エラー:私は<strong>POST</strong>私<strong>JSON</strong>にしようとしたときにPythonのPOSTリクエスト

schema = { 
    '_update': { 
     'type': 'datetime', 
     'minlength': 1, 
     'maxlength': 40, 
     'required': False, 
     'unique': True, 
    }, 
    'Name': { 
     'type': 'string', 
     'minlength': 1, 
     'maxlength': 40, 
     'required': True, 
     'unique': False, 
    }, 
    'FacebookId': { 
     'type': 'integer', 
     'minlength': 1, 
     'maxlength': 40, 
     'required': True, 
     'unique': True, 
    }, 
    'HighScore': { 
     'type': 'integer', 
     'minlength': 1, 
     'maxlength': 40, 
     'required': True, 
     'unique': False, 
    }, 
} 

は、私はポストにしようとしていますJSONである:ここでは

{"_updated":null,"Name":"John Doe","FacebookId":"12453523434324123","HighScore":15} 

は私からPOST要求を実行しようとしています方法ですクライアント:

IDictionary dict = Facebook.MiniJSON.Json.Deserialize(result.RawResult) as IDictionary; 
string name = dict["name"].ToString(); 
string id = dict["id"].ToString(); 

Player player = new Player(); 
player.FacebookId = id; 
player.Name = name; 
player.HighScore = (int) GameManager.Instance.Points; 

// Using Newtonsoft.Json to serialize 
var json = JsonConvert.SerializeObject(player); 

var headers = new Dictionary<string, string> {{"Content-Type", "application/json"}}; 

string url = "http://server.com/Players"; 
var encoding = new UTF8Encoding(); 
// Using Unity3d WWW class 
WWW www = new WWW(url, encoding.GetBytes(json), headers); 
StartCoroutine(WaitForRequest(www)); 

答えて

1

あなたは非常に複雑です。まず、あなたのサービスを呼び出すためにヘルパーメソッドが必要です。このような何か:

private static T Call<T>(string url, string body) 
{ 
    var contentBytes = Encoding.UTF8.GetBytes(body); 
    var request = (HttpWebRequest)WebRequest.Create(url); 

    request.Timeout = 60 * 1000; 
    request.ContentLength = contentBytes.Length; 
    request.Method = "POST"; 
    request.ContentType = @"application/json"; 

    using (var requestWritter = request.GetRequestStream()) 
     requestWritter.Write(contentBytes, 0, (int)request.ContentLength); 

    var responseString = string.Empty; 
    var webResponse = (HttpWebResponse)request.GetResponse(); 
    var responseStream = webResponse.GetResponseStream(); 
    using (var reader = new StreamReader(responseStream)) 
     responseString = reader.ReadToEnd(); 

    return JsonConvert.DeserializeObject<T>(responseString); 
} 

、単にこのようにそれを呼び出す:

var url = "http://server.com/Players"; 
var output=Call<youroutputtype>(url, json); 

注:私はちょうどあなたにそれを残すように、私はあなたの出力タイプが何であるかを知りませんでした。

関連する問題

 関連する問題