REST 中不允许使用 HTTP 方法 API Post
HTTP Method Not Allowed in REST API Post
我正在使用 UnityWebRequest POST JSON 上可在线访问的字符串。不幸的是,我在 Unity 中收到 HTTP/1.1 405 Method Not Allowed
错误。这绝对不是 API 键错误,否则我会收到未经授权的消息。
我看过一些使用 PUT 而不是 POST 的例子,所以我不确定我在这里为 POST 所做的事情是否正确。请帮助我。
IEnumerator POSTURL()
{
WWWForm form = new WWWForm();
form.AddField("ID", "Lemon");
using (UnityWebRequest request = UnityWebRequest.Post("website_url", form))
{
request.SetRequestHeader("api-key", KEY);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
}
{
"ID": "Orange",
"Category": "Fruits",
}
仍然只是猜测,但更容易解释我的意思^^
您可能必须使用 UnityWebRequest.Put
// You could pass these as parameters dynmically
IEnumerator POSTURL(string id, string category)
{
// This is string interpolation and will dynamically fill in the
// id and category value
var data = Encoding.UTF8.GetBytes($"{{\"ID\":\"{id}\",\"Category\":\"{category}\"}}");
using (UnityWebRequest request = UnityWebRequest.Put("website_url", data))
{
request.SetRequestHeader("api-key", KEY);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
}
或者如前所述,您也可以遵循 并使用 Post
,但没有表格,而是直接使用原始数据
var data = Encoding.UTF8.GetBytes($"{{\"ID\":\"{id}\",\"Category\":\"{category}\"}}");
UnityWebRequest webRequest = UnityWebRequest.Post(uri, "");
// Fix: Add upload handler and pass json as bytes array
webRequest.uploadHandler = new UploadHandlerRaw(data);
webRequest.SetRequestHeader("Content-Type", "application/json");
yield return webRequest.SendWebRequest();
我正在使用 UnityWebRequest POST JSON 上可在线访问的字符串。不幸的是,我在 Unity 中收到 HTTP/1.1 405 Method Not Allowed
错误。这绝对不是 API 键错误,否则我会收到未经授权的消息。
我看过一些使用 PUT 而不是 POST 的例子,所以我不确定我在这里为 POST 所做的事情是否正确。请帮助我。
IEnumerator POSTURL()
{
WWWForm form = new WWWForm();
form.AddField("ID", "Lemon");
using (UnityWebRequest request = UnityWebRequest.Post("website_url", form))
{
request.SetRequestHeader("api-key", KEY);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
}
{
"ID": "Orange",
"Category": "Fruits",
}
仍然只是猜测,但更容易解释我的意思^^
您可能必须使用 UnityWebRequest.Put
// You could pass these as parameters dynmically
IEnumerator POSTURL(string id, string category)
{
// This is string interpolation and will dynamically fill in the
// id and category value
var data = Encoding.UTF8.GetBytes($"{{\"ID\":\"{id}\",\"Category\":\"{category}\"}}");
using (UnityWebRequest request = UnityWebRequest.Put("website_url", data))
{
request.SetRequestHeader("api-key", KEY);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
Debug.Log(request.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
}
或者如前所述,您也可以遵循 Post
,但没有表格,而是直接使用原始数据
var data = Encoding.UTF8.GetBytes($"{{\"ID\":\"{id}\",\"Category\":\"{category}\"}}"); UnityWebRequest webRequest = UnityWebRequest.Post(uri, ""); // Fix: Add upload handler and pass json as bytes array webRequest.uploadHandler = new UploadHandlerRaw(data); webRequest.SetRequestHeader("Content-Type", "application/json"); yield return webRequest.SendWebRequest();