1

Unity3Dゲームエンジンのfirebaseクラウド機能プロジェクトにPOST Httpリクエストを作成する際に問題があります。UnityWebRequest Firebaseのクラウド機能へのHTTP POST

私はコード400応答を取得しておくと、私は次のエラーを確認できfirebaseコンソールで:

Error: invalid json at parse

を私は本当にHTTPリクエストに関する多くの知識を持っており、かなりの時間後にはありません私が助けを求めたい解決策を見つけようとするのです。ここで

は、クライアントのコードです:

public void RateLevel(string guid, int rating) 
{ 
    RateLevelRequest rlr = new RateLevelRequest (guid, rating.ToString()); 
    string body = rlr.ToJson(); 
    UnityWebRequest www = UnityWebRequest.Post("myurl", body); 
    www.SetRequestHeader ("Content-Type", "application/json"); 
    StartCoroutine (MakeRequest (www)); 
} 

/* * * * * * * * * * * * * * * * * * * 
* AUXILIAR CLASS FOR HTTP REQUESTS * 
* * * * * * * * * * * * * * * * * * */ 

[System.Serializable] 
public class RateLevelRequest 
{ 
    public string guid; 
    public string rating; 

    public RateLevelRequest(string _guid, string _rating) 
    { 
     guid = _guid; 
     rating = _rating; 
    } 

    public string ToJson() 
    { 
     string json = JsonUtility.ToJson (this); 
     Debug.Log ("RateLevelRequest Json: " + json); 
     return json; 
    } 
} 

私はこのような値を使用して、JSONがよく形成されることを保証することができます。

{"guid":"fake-guid","rating":"-1"}

そして、ここにfirebase-functionsの私の現在の展開された機能があります。

exports.rate_level = functions.https.onRequest((req, res) => { 
    if(req.method === 'POST') 
    { 
     console.log('guid: ' + req.body.guid); 
     console.log('rating: ' + req.body.rating); 
     console.log('invented var: ' + req.body.myinvention); 

     if(req.body.guid && req.body.rating && 
     (req.body.rating == 1 || req.body.rating == -1)) 
     { 
      res.status(200).send('You are doing a post request with the right fields and values'); 
     } 
     else 
     { 
      res.status(403).send('Required Fields are not Defined!') 
     } 
    } 
    else 
    { 
     res.status(403).send('Wrong Request Method!'); 
    } 
}); 

誰もこれを試して成功しましたか?

ありがとうございます!

答えて

2

Ok回答はexcellent blog entryです。

本当に何が間違っているのか分かりませんが、代わりに私のコードを上記の記事に記載されているコードに置き換えました。あなたの残りの人に問題があるのを投稿します。

public void RateLevel(string guid, int rating) 
    { 
     RateLevelRequest rlr = new RateLevelRequest (guid, rating.ToString()); 
     string body = rlr.ToJson(); 
     byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes (body); 
     UnityWebRequest www = new UnityWebRequest("myurl", UnityWebRequest.kHttpVerbPOST); 
     www.uploadHandler = (UploadHandler)new UploadHandlerRaw (bodyRaw); 
     www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); 
     www.SetRequestHeader ("Content-Type", "application/json"); 
     StartCoroutine (MakeRequest (www)); 
    } 

ベスト!

関連する問題