我正在使用 Ruby on Rails 应用程序中的 HTTparty gem 向端点发出 POST 请求。
我在 POST 请求中将请求正文作为 json 传递。
request_body = {}
request_body[:name] = "John"
request_body[:age] = 26
request_body[:revenue] = [{id: "price"}, {id: "tax"}}]
request_body[:contact] = [{id: "email"}, {id: "phone"}}]
我正在为多个请求创建上述请求正文结构。如何以更好的方式形成请求正文?
有没有办法通过只传递值(例如价格和税)而不是像下面那样使用来创建哈希数组
request_body[:revenue] = [{id: "price"}, {id: "tax"}}]
下面是我的场景,我想传递哈希值,方法将返回哈希数组。
输入:
attributes = %w(price tax)
array_of_hashes(attributes)
预期输出:
def array_of_hashes()
[{id: "price"}, {id: "tax"}}]
end
你可以试试这个
def array_of_hashes array
array.map{ |el| {id: el} }
end
和
array_of_hashes(["price", "tax"]) -> returns [{ id: "price" }, {id: "tax"}]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句