gpt4 book ai didi

ruby-on-rails - 解析从 Unity 发送到 Ruby On Rails 的 JSON POST 数据

转载 作者:可可西里 更新时间:2023-11-01 17:26:28 29 4
gpt4 key购买 nike

我正在为 Unity 前端构建 Rails 服务。其中一个 Controller 在解析来 self 的 UnityWebRequest 的 JSON blob 时遇到问题.以下是 Unity 所说的发送内容:

[StoryPost] Sending request to localhost:3000/stories/new with post data [words, {"_array":[{"_text":"mailing"}]}]

这是 Rails 在收到 JSON 时记录的内容:

Started POST "/stories/new" for 127.0.0.1 at 2017-08-26 20:08:40 -0400
Error occurred while parsing request parameters.
Contents:

words=%7B%22_array%22%3A%5B%7B%22_text%22%3A%22mailing%22%7D%5D%7D

ActionDispatch::Http::Parameters::ParseError (743: unexpected token at 'words=%7B%22_array%22%3A%5B%7B%22_text%22%3A%22mailing%22%7D%5D%7D'):

actionpack (5.1.3) lib/action_dispatch/http/parameters.rb:113:in `rescue in parse_formatted_parameters'
actionpack (5.1.3) lib/action_dispatch/http/parameters.rb:107:in `parse_formatted_parameters'

看起来我有某种编码错误。一旦我将以下代码添加到我的网络请求中,这种情况就开始发生:

_request.SetRequestHeader("Content-Type", "application/json");
_request.SetRequestHeader("Accept", "application/json");

如果我注释掉这两个 SetRequestHeader上面提到的调用,那么这里是 Rails 记录的内容:

Started POST "/stories/new" for 127.0.0.1 at 2017-08-26 23:13:14 -0400
Processing by StoriesController#new as */*
Parameters: {"words"=>"{\"_array\":[{\"_text\":\"inspectors\"}]}"}

我添加了这段代码,以便 Rails 自动知道将我的 POST 数据解析为 JSON。有人以前见过这种类型的问题吗? Rails 和 HTTP 的新手,但不是 Unity 的新手。

相关代码

作为引用,这是我的 Rails Controller :

class StoriesController < ApplicationController      

def new
post_data = params["words"]
words_array = post_data["_array"];
puts "words " + words_array
story = Story.new(words_array)
story.save()
end
end

这是我的 Unity 代码。注:_PostDataDictionary<string, string> .

protected ServiceCall()
{
switch (_Type)
{
case RequestType.Get:
_request = UnityWebRequest.Get(_URL);
break;
case RequestType.Post:
_request = UnityWebRequest.Post(_URL, _PostData);
break;
default:
throw new NullReferenceException("Unknown request type " + _Type);
}

_request.SetRequestHeader("Content-Type", "application/json");
_request.SetRequestHeader("Accept", "application/json");

_request.downloadHandler = new DownloadHandlerBuffer();

Diag.Crumb(this, $"Sending request to {_URL} with post data {_PostData.Pretty()}");

_requestOperation = _request.Send();
}

这是我正在构建的方式 _PostData

    protected override Dictionary<string, string> _PostData 
{
get
{
var asArray = new JsonArray<Word>(Luke.GuessWords);
string json = JsonUtility.ToJson(asArray);
return new Dictionary<string, string>{{"words", json}};
}
}

JSONArray只是一个简单的类,它有一个 _array字段,因为 Unity 的 JsonUtility无法序列化顶级数组。

最佳答案

为了响应反馈,我的整个 POST 正文未格式化为 JSON,我改为将 _PostData 更改为返回字符串,并在我的派生类中将 _PostData 序列化为JSON。然后在基类中,我将 JSON 编码为字节并将其作为 POST 正文发送。这是我更改后的代码:

在 Rails 中:

def new
words_array = params["_array"]
puts "words " + words_array.to_s
story = Story.new(words_array)
puts "story is " + story.to_s
end

在 Unity 中:

protected override string _PostData 
{
get
{
var asArray = new JsonArray<Word>(Luke.GuessWords);
string json = JsonUtility.ToJson(asArray);
return json;
}
}

还有……

case RequestType.Post:
_request = UnityWebRequest.Post(_URL, new Dictionary<string, string>());
_request.uploadHandler = new UploadHandlerRaw
(
Encoding.UTF8.GetBytes(_PostData)
);
break;

关于ruby-on-rails - 解析从 Unity 发送到 Ruby On Rails 的 JSON POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45900921/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com