簡體   English   中英

使用我的 firebase URL 獲得 400(錯誤請求)

[英]Getting 400 (Bad Request) with my firebase URL

I am coming to an issue where I am trying to do a PUT request to update and store data to my firebase database, but for some reason when I run my code and select on my object and click on next, I get error with my firebase web 請求 url 作為 400(錯誤請求)錯誤。 有沒有人如何解決這個問題。 謝謝您的幫助。

PS:HIDDEN_URL 只是一個文本 - 出於安全目的,我隱藏了我的項目 url。 謝謝!

- 硬編碼值起作用: "{\"messages\":{\"message\":\"This is a test\"}}"; 但不是lastHighlightedObject.GetComponent<OBClick>().name;

這是我的代碼:

    public void NextButton()
    {
        if (highlightSet == true)
        {
             var httpWebRequest =
                (HttpWebRequest) WebRequest.Create("https://HIDDEN_URL.firebaseio.com/brokenComp.json");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "PUT";


            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {

                 string missingObjectCount = lastHighlightedObject.GetComponent<OBClick>().name;
                 streamWriter.Write(missingObjectCount);

            }

            var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                Debug.Log(result);

            } 

            // When everything is Okay, it will load the scene. 
            SceneManager.LoadScene("Quiz");
        }

該服務似乎需要某種 JSON 格式。 由於您的核心版本有效

"{\"messages\":{\"message\":\"This is a test\"}}";

但不是一個string我猜你必須使用類似的東西

 string missingObjectCount = "{\"messages\":{\"message\":\"" + HighlightedObject.GetComponent<OBClick>().name + "\"}}"

您之前的問題所述:

目前,您的代碼將凍結,直到請求完成。

我寧願使用UnityWebRequest.Put在請求完成之前不阻塞整個線程。

這只是 API 中的示例(稍作修改),但我想它非常簡單,您應該可以將它用於您的目的

public class MyBehavior : MonoBehaviour 
{
    public void NextButton()
    {
        StartCoroutine(Upload());
    }

    IEnumerator Upload()
    {
        byte[] myData = System.Text.Encoding.UTF8.GetBytes("{\"messages\":{\"message\":\"" + HighlightedObject.GetComponent<OBClick>().name + "\"}}");
        using (UnityWebRequest www = UnityWebRequest.Put(YOUR_URL, myData))
        {
            yield return www.Send();

            if (www.isNetworkError || www.isHttpError)
            {
                Debug.Log(www.error);
            }
            else
            {
                Debug.Log("Upload complete!");
            }
        }
    }
}

順便說一句,阻礙您使用Firebase SDK的東西?

會有類似的東西

public class MyScript: MonoBehaviour 
{ 
    private void Start() 
    {
        // Set up the Editor before calling into the realtime database.
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-FIREBASE-APP.firebaseio.com/");

        // Get the root reference location of the database.
        reference = FirebaseDatabase.DefaultInstance.RootReference;
    }

    public void NextButton()
    {
        // ported the variable names from your latest question
        mDatabaseRef.Child("messages").Child("message").SetValueAsync(HighlightedObject.GetComponent<OBClick>().name);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM