Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用HttpParty发送JSON POST请求中的数组_Ruby On Rails_Ruby_Json_Httparty - Fatal编程技术网

Ruby on rails 使用HttpParty发送JSON POST请求中的数组

Ruby on rails 使用HttpParty发送JSON POST请求中的数组,ruby-on-rails,ruby,json,httparty,Ruby On Rails,Ruby,Json,Httparty,我正在尝试使用第三方API,该API要求在POST请求主体中发送一个数组。我已经掌握了发送JSON的窍门;我已经读过了,你只需要设置一些标题,并在帖子正文上调用到_json。但是,我不知道如何在文章正文中嵌入数组。我尝试了以下方法: HTTParty.post(url, :body => { :things => [{:id => 1}, {:id => 2}, {:id => 3}], }.to_json, :heade

我正在尝试使用第三方API,该API要求在POST请求主体中发送一个数组。我已经掌握了发送JSON的窍门;我已经读过了,你只需要设置一些标题,并在帖子正文上调用
到_json
。但是,我不知道如何在文章正文中嵌入数组。我尝试了以下方法:

HTTParty.post(url, 
    :body => { 
        :things => [{:id => 1}, {:id => 2}, {:id => 3}],
    }.to_json,
    :headers => { 
        'Content-Type' => 'application/json', 
        'Accept' => 'application/json'
    }
)
#<HTTParty::Response:0x10 parsed_response=nil, 
@response=#<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>, 
@headers={"error_message"=>["Can not deserialize instance of java.lang.Long out of 
START_OBJECT token  at [Source: org.apache.catalina.connector.CoyoteInputStream@30edd11c; 
line: 1, column: 15] (through reference chain: REDACTED[\"things\"])"], 
"error_code"=>["0"], "content-length"=>["0"], 
"date"=>["Wed, 13 Aug 2014 22:53:49 GMT"], "connection"=>["close"]}> 
但这给了我一个服务器错误,使我相信数组的格式不正确。有人能建议如何在JSON POST请求中发送数组吗?谢谢

编辑:

我返回的错误如下:

HTTParty.post(url, 
    :body => { 
        :things => [{:id => 1}, {:id => 2}, {:id => 3}],
    }.to_json,
    :headers => { 
        'Content-Type' => 'application/json', 
        'Accept' => 'application/json'
    }
)
#<HTTParty::Response:0x10 parsed_response=nil, 
@response=#<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>, 
@headers={"error_message"=>["Can not deserialize instance of java.lang.Long out of 
START_OBJECT token  at [Source: org.apache.catalina.connector.CoyoteInputStream@30edd11c; 
line: 1, column: 15] (through reference chain: REDACTED[\"things\"])"], 
"error_code"=>["0"], "content-length"=>["0"], 
"date"=>["Wed, 13 Aug 2014 22:53:49 GMT"], "connection"=>["close"]}> 

我对SurveyMonkey API也有类似的要求,下面将创建一个带有嵌套哈希数组的params_哈希

fields = []

i =0

while i < 10 do

fields << {":id#{i}" => "some value #{i}"}

i += 1

end
创建散列的字段数组

fields = []

i =0

while i < 10 do

fields << {":id#{i}" => "some value #{i}"}

i += 1

end

在RubyonRails中使用HttpParty在POST正文中嵌入数组的最简单方法是将请求传递给实例变量(您选择的任何名称都可以满足实例变量的要求)

所以我们会

@mypost = HTTParty.post(url, 
         :body => { 
                    :things => {
                       :id => 1, 
                       :id => 2,
                       :id => 3
                    },
                  }.to_json,
         :headers => { 
                    'Content-Type' => 'application/json',
                    'Authorization' => 'xxxxxxxxxx' 
                    'Accept' => 'application/json'
                    })
这里是一个HTTParty Post请求的示例

 @myrequest = HTTParty.post(' https://www.pingme.com/wp-json/wplms/v1/user/register',
            :body => {
                        :books => {  
                          :name => "#{@book.name}",
                          :author => "#{@book.author}",
                          :description => "#{@book.description}",
                          :category_id => "#{@book.category_id}",
                          :sub_category_id => "#{@book.sub_category_id}"
                        },
                      }.to_json, 
            :headers => { 'Content-Type' => 'application/json', 
                          'Authorization' => '77d22458349303990334xxxxxxxxxx',
                          'Accept' => 'application/json'})
就这些


我希望这有帮助。

我觉得你的代码总体上还不错。JSON将是有效的(并且将包含一个数组),但可能您没有以正确的API结构发送数据。你能给我一个API文档中的参考或者一个正确的JSON示例吗?另外,如果相关的话,请给出服务器错误消息。我已经按照你的要求更新了帖子!由于我第一次犯了一个错误,我也稍微更改了我的代码。钥匙
:myid
是故意的还是必需的?它与您的示例JSON不匹配。糟糕,这是一个输入错误。但与问题无关。这可能会有所帮助。默认情况下,
HTTParty
用于规范化请求。尝试使用覆盖阵列的此行为。