使用 Ruby/Rails 发布多个对象(JSON 格式错误?)

发布于 2024-11-29 19:24:18 字数 1229 浏览 3 评论 0原文

这更像是一个“我做这个正确的问题吗”。

基本上我已经完成了标准的客户端服务器设置。

我正在尝试将多个对象作为一个对象发布到服务器。发布的对象是:

1:一维字符串数组(非常简单)@headers

2:哈希数组,每个哈希数组包含大约 7 个值和键。 @contentsArray

我想像下面这样的东西就可以解决问题

    @postedInfo = {:info =>
      {
        :headers =>@headers,
        :content => @contentsArray
      }
    }
    myJsonReq = @postedInfo

    puts "ITS A MAAAAAAAAAAADDDDD HOUUUUUSSSSSEEEEE" #Sorry, I just saw rise of the planet of the apes
    puts myJsonReq.as_json
    res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3007/update.json'),myJsonReq)

定义的URL显然是服务器,但它的实现方式如下

format: json
action: update_repo
pages: "content Page Title ........."very long string with no brackets or resemblance of JSON" followed by the headers part
headers: all strings are in here
controller: Update

如果我创建一个像这样的变量

@x = (params[:pages])
and puts @x.class

@x是一个字符串,在其他代码位中它将通过作为一个对象。具有无关访问权限的数组或哈希。

当然,我认为我必须从 JSON 反序列化它,所以我使用了这

JSON.parse(@x)
JSON.parse(params)

两个行,这两个行都抛出 JSON::ParserError (745: Unexpected token at 'content .... 然后是字符串的其余部分。

我想我正在发送对象是正确的还是我构建它们的方式错误?

This is more of an "am i doing this right question".

Basically I have your standard client server setup going.

I'm trying to post multiple objects as one to the server. The objects being posted are:

1: A 1d array of strings (pretty straightforward) @headers

2: An array of hashes each of which contain about 7 values and keys. @contentsArray

I figures something like the following would do the trick

    @postedInfo = {:info =>
      {
        :headers =>@headers,
        :content => @contentsArray
      }
    }
    myJsonReq = @postedInfo

    puts "ITS A MAAAAAAAAAAADDDDD HOUUUUUSSSSSEEEEE" #Sorry, I just saw rise of the planet of the apes
    puts myJsonReq.as_json
    res = Net::HTTP.post_form(URI.parse('http://127.0.0.1:3007/update.json'),myJsonReq)

The URL defined is obviously the server but how it comes through is like the following

format: json
action: update_repo
pages: "content Page Title ........."very long string with no brackets or resemblance of JSON" followed by the headers part
headers: all strings are in here
controller: Update

If I create a variable like so

@x = (params[:pages])
and puts @x.class

@x is a string where in other bits of code It would come through as an object. Either array or hash with indifferent access.

Naturally I thought that I has to deserialize it from JSON so I used the line's

JSON.parse(@x)
JSON.parse(params)

both of which threw JSON::ParserError (745: unexpected token at 'content .... and then the rest of the string.

I think I'm sending the objects right or am I constructing them the wrong way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2024-12-06 19:24:18

来自精细手册

表单数据必须表示为字符串到字符串的哈希

您正在传递符号到哈希值的哈希值。如果您深入研究源代码,您会发现它最终会经历以下过程:

def set_form_data(params, sep = '&')
  self.body = params.map {|k, v| encode_kvpair(k, v) }.flatten.join(sep)
  self.content_type = 'application/x-www-form-urlencoded'
end

def encode_kvpair(k, vs)
  Array(vs).map {|v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }
end

如果 vs 是一个哈希(如您的情况),您将不会得到任何有用的信息。例如,将所有这些应用于 {:a =>; {:b => :c}} 你会得到这个:

"a=[:b,%20:c]"

这并不是很有用。

因此,如果您想使用 Net::HTTP.post_form,那么您必须自己将数据扁平化为简单的一级哈希。

From the fine manual:

Form data must be represented as a Hash of String to String

You're passing in a Hash of Symbol to Hash. If you dig into the source, you'll see that it ends up going through this:

def set_form_data(params, sep = '&')
  self.body = params.map {|k, v| encode_kvpair(k, v) }.flatten.join(sep)
  self.content_type = 'application/x-www-form-urlencoded'
end

def encode_kvpair(k, vs)
  Array(vs).map {|v| "#{urlencode(k.to_s)}=#{urlencode(v.to_s)}" }
end

If the vs is a Hash (as in your case), you won't get anything useful coming through. For example, apply all that to {:a => {:b => :c}} you'll get this:

"a=[:b,%20:c]"

And that's not terribly useful.

So, if you want to use Net::HTTP.post_form, then you'll have to flatten your data into a simple one level Hash yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文