[英]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 request url as a 400 (Bad Request) error. 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(错误请求)错误。 does anyone how to solve this issue.
有没有人如何解决这个问题。 thanks for the help.
谢谢您的帮助。
PS: HIDDEN_URL is just a text - I hide my project url for security purposes. PS:HIDDEN_URL 只是一个文本 - 出于安全目的,我隐藏了我的项目 url。 Thanks!
谢谢!
-The hardcode values work: "{\"messages\":{\"message\":\"This is a test\"}}";
- 硬编码值起作用:
"{\"messages\":{\"message\":\"This is a test\"}}";
but not lastHighlightedObject.GetComponent<OBClick>().name;
但不是
lastHighlightedObject.GetComponent<OBClick>().name;
Here is my code:这是我的代码:
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");
}
The service seems to require a certain JSON format.该服务似乎需要某种 JSON 格式。 Since your hardcore version works
由于您的核心版本有效
"{\"messages\":{\"message\":\"This is a test\"}}";
but not a single string
I guess you have to use something like但不是一个
string
我猜你必须使用类似的东西
string missingObjectCount = "{\"messages\":{\"message\":\"" + HighlightedObject.GetComponent<OBClick>().name + "\"}}"
As mentioned in your previous question :如您之前的问题所述:
Currently your code will freeze until the request is done.目前,您的代码将冻结,直到请求完成。
I would rather use either UnityWebRequest.Put
to not block the entire thread until the request is done.我宁愿使用
UnityWebRequest.Put
在请求完成之前不阻塞整个线程。
This is just the example (with small edits) from the API but I guess it js pretty straight forward and you should be able to use it for your purpose这只是 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!");
}
}
}
}
Btw is something hindering you from using the Firebase SDK ?顺便说一句,阻碍您使用Firebase SDK的东西?
There it would be something like会有类似的东西
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.