【发布时间】: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