如何 post 我的数据使用 UnityWebRequest post api 调用
how to post my data using UnityWebRequest post api call
这是我的API请求
public IEnumerator Login(string bodyJsonString)
{
Debug.Log(bodyJsonString);
UnityWebRequest req = UnityWebRequest.Post("localhost:3000/login", bodyJsonString);
req.SetRequestHeader("content-type", "application/json");
yield return req.SendWebRequest();
if (req.isNetworkError || req.isHttpError)
{
Debug.Log(req.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
它 returns 一个错误状态代码 500 并且在服务器上 returns 一个错误 Unexpected token % in JSON at position 0","severity
这是我的协程调用
public void submitLogin()
{
_username = userInputField.GetComponent<InputField>().text;
_password = passwordInputField.GetComponent<InputField>().text;
Debug.Log("username" + _username);
Debug.Log("password" + _password);
string body = "{'username':'" + _username + "','password','" + _password + "'}";
//API Call
authChexi = new Auth();
StartCoroutine(authChexi.Login(body));
}
如果您对如何处理我的表单正文有任何想法,请告诉我。谢谢
所以我更新了我的功能。我做了一些挖掘并最终解决了它。我的错误确实是手动构建了一个 JSON。所以这是我的解决方案。
public void submitLogin()
{
_username = userInputField.GetComponent<InputField>().text;
_password = passwordInputField.GetComponent<InputField>().text;
//API Call
authChexi = new Auth();
StartCoroutine(authChexi.Login(_username, _password));
}
为我的 json 对象创建了一个 class 用户数据
public class UserData
{
public string username;
public string password;
public string email;
}
并调用 API
public IEnumerator Login(string username, string password)
{
//@TODO: call API login
// Store Token
// Add Token to headers
var user = new UserData();
user.username = username;
user.password = password;
string json = JsonUtility.ToJson(user);
var req = new UnityWebRequest("localhost:3000/login", "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
req.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
req.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
//Send the request then wait here until it returns
yield return req.SendWebRequest();
if (req.isNetworkError)
{
Debug.Log("Error While Sending: " + req.error);
}
else
{
Debug.Log("Received: " + req.downloadHandler.text);
}
}
现在它运行得非常棒!
这是我的API请求
public IEnumerator Login(string bodyJsonString)
{
Debug.Log(bodyJsonString);
UnityWebRequest req = UnityWebRequest.Post("localhost:3000/login", bodyJsonString);
req.SetRequestHeader("content-type", "application/json");
yield return req.SendWebRequest();
if (req.isNetworkError || req.isHttpError)
{
Debug.Log(req.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
它 returns 一个错误状态代码 500 并且在服务器上 returns 一个错误 Unexpected token % in JSON at position 0","severity
这是我的协程调用
public void submitLogin()
{
_username = userInputField.GetComponent<InputField>().text;
_password = passwordInputField.GetComponent<InputField>().text;
Debug.Log("username" + _username);
Debug.Log("password" + _password);
string body = "{'username':'" + _username + "','password','" + _password + "'}";
//API Call
authChexi = new Auth();
StartCoroutine(authChexi.Login(body));
}
如果您对如何处理我的表单正文有任何想法,请告诉我。谢谢
所以我更新了我的功能。我做了一些挖掘并最终解决了它。我的错误确实是手动构建了一个 JSON。所以这是我的解决方案。
public void submitLogin()
{
_username = userInputField.GetComponent<InputField>().text;
_password = passwordInputField.GetComponent<InputField>().text;
//API Call
authChexi = new Auth();
StartCoroutine(authChexi.Login(_username, _password));
}
为我的 json 对象创建了一个 class 用户数据
public class UserData
{
public string username;
public string password;
public string email;
}
并调用 API
public IEnumerator Login(string username, string password)
{
//@TODO: call API login
// Store Token
// Add Token to headers
var user = new UserData();
user.username = username;
user.password = password;
string json = JsonUtility.ToJson(user);
var req = new UnityWebRequest("localhost:3000/login", "POST");
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
req.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
req.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
//Send the request then wait here until it returns
yield return req.SendWebRequest();
if (req.isNetworkError)
{
Debug.Log("Error While Sending: " + req.error);
}
else
{
Debug.Log("Received: " + req.downloadHandler.text);
}
}
现在它运行得非常棒!