【问题标题】:Rails how to build a custom json request in my helperRails 如何在我的助手中构建自定义 json 请求
【发布时间】:2014-04-12 07:41:31
【问题描述】:

我正在使用 API,并尝试在帮助程序中发布以下请求

module ShoppingCartHelper

def update_order
  HTTParty.post("APIURL",
            :body => {
            @all_lines.each do |line|
            :ProductCode => line.ProductCode,
            :Quantity => line.Quantity,
            :UnitPrice => line.UnitPrice,
            :Pages =>
            [
                {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                        { 
                            :AssetNumber => line.AssetNumber,
                            :Name => line.Name,
                            :HiResImage => line.HiResImage, 
                            :CropMode => line.CropMode
                        }
                    ]
                }
            ]
        end
        }.to_json,
      :headers => { 'Content-Type' => 'application/json' })
end

end

当我这样做时,我会遇到各种语法错误和意外字符。 @all_lines 是数据库中订单的所有行,我正在尝试为其找到的每一行构建该 json 正文。请帮助!。

我没有使用 jbuilder 之类的东西,因为我使用的所有这些字段都只在一个表中.. 它们没有链接到我可以包含它们的位置。除非有其他方法可以创建自定义标签

更新:我需要这个结构才能工作,但我收到语法错误:

  HTTParty.post("APIURL",
            :body => 
            "Lines" =>
            [
            @all_lines.map do |line|
             {
               :ProductCode => line.ProductCode,
               :Quantity => line.Quantity,
               :UnitPrice => line.UnitPrice,
               :Pages =>
               [
                  {
                    :PageNumber => line.PageNumber,
                    :Assets =>
                    [
                      { 
                        :AssetNumber => line.AssetNumber,
                        :Name => line.Name,
                        :HiResImage => line.HiResImage, 
                        :CropMode => line.CropMode
                      }
                    ]
                  }
               ]
            } 
          end.to_json],
      :headers => { "Content-Type" => "application/json"})

来自 API 的请求正文需要如下所示:

"Lines":
    [
        {
            "ProductCode":"5x7",
            "Quantity":2,
            "UnitPrice":1.25,
            "Pages":
            [
                {
                    "PageNumber":1,
                    "Assets":
                    [
                        {
                            "AssetNumber":1,
                            "Name":"000-R20110419153212.jpg",
                            "HiResImage":"http:\/\/www.google.com\/xyz.jpg",
                            "CropMode":"FILLIN"
                        }
                    ]
                }
            ]
        }
    ]

【问题讨论】:

    标签: ruby-on-rails ruby json post httparty


    【解决方案1】:

    each API 返回数组本身(未经修改)。您可能想使用map,它返回一个包含块结果的数组:

    [1, 2, 3].each { |x| x*2 }
    # => [1, 2, 3]
    [1, 2, 3].map { |x| x*2 }
    # => [2, 4, 6]
    

    所以试试(你也错过了你的块中哈希结果的花括号的位置):

    def update_order
      HTTParty.post("APIURL",
                :body => 
                @all_lines.map do |line|
                 {
                   :ProductCode => line.ProductCode,
                   :Quantity => line.Quantity,
                   :UnitPrice => line.UnitPrice,
                   :Pages =>
                   [
                      {
                        :PageNumber => line.PageNumber,
                        :Assets =>
                        [
                          { 
                            :AssetNumber => line.AssetNumber,
                            :Name => line.Name,
                            :HiResImage => line.HiResImage, 
                            :CropMode => line.CropMode
                          }
                        ]
                      }
                   ]
                } 
              end.to_json,
          :headers => { 'Content-Type' => 'application/json' })
    end
    

    使用“Lines”包装器:

    HTTParty.post("APIURL",
                :body => 
                {"Lines" =>
                [
                @all_lines.map do |line|
                 {
                   :ProductCode => line.ProductCode,
                   :Quantity => line.Quantity,
                   :UnitPrice => line.UnitPrice,
                   :Pages =>
                   [
                      {
                        :PageNumber => line.PageNumber,
                        :Assets =>
                        [
                          { 
                            :AssetNumber => line.AssetNumber,
                            :Name => line.Name,
                            :HiResImage => line.HiResImage, 
                            :CropMode => line.CropMode
                          }
                        ]
                      }
                   ]
                } 
              end]}.to_json,
          :headers => { "Content-Type" => "application/json" })
    

    【讨论】:

    • 您好,感谢您的回复!希望你能多帮忙一点。我错过了 API 中的一个结构,我需要在顶部使用 Lines,但我又遇到了语法错误:
    • 啊谢谢!!你能不能也编辑掉 Apikey 部分哈哈,忘了把它取下来
    猜你喜欢
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多