如何通过卷曲后的数组值? [英] How to post array values via curl?

查看:226
本文介绍了如何通过卷曲后的数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢测试其作为显示在下面的示例设计的API后端

 的http://本地主机:3000 / API / V1 /店/ 1.json

JSON响应:

  {
  ID:1,
  名称:超市
  产品:
    水果,
    蛋
  ]
}

下面是相应的模型:

 #应用程序/模型/ shop.rb
类店< ActiveRecord的::基地
  延长Enumerize
  attr_accessible:姓名,:产品  连载:产品阵列
  enumerize:产品中:%W {水果肉类蛋类},多:真  resourcify  验证:姓名,presence:真的,长度:{中:5..50}
  验证:产品,presence:真
结束

我想使用的卷曲的测试创建和更新条目。因此,我用下面的命令:

创建

  $卷曲-X POST的http://本地主机:3000 / API / V1 / shops.json -d \\
  店[名] =超级市场及放大器;店[产品] =水果,鸡蛋和放大器; AUTH_TOKEN = a1b2c3d4

更新:

  $卷曲-X PUT HTTP://本地主机:3000 / API / V1 /店/ 1.json -d \\
  店[名] =超级市场及放大器;&安培;店[产品] =水果,鸡蛋和放大器; AUTH_TOKEN = a1b2c3d4

产品的价值需要提交作为数组。当我运行上面的命令将返回以下信息:

  {错误:{产品:无效]}

我如何需要写数组所以它的工作原理与产品价值的卷曲的?


解决方案

  $卷曲-X POST的http://本地主机:3000 / API / V1 / shops.json -d \\
  店[名] =超市\\
  &安培;店[产品] [] =水果\\
  &安培;店[产品] [] =蛋\\
  &安培; AUTH_TOKEN = a1b2c3d4

I like to test an API backend which is designed as shown in the following example:

http://localhost:3000/api/v1/shops/1.json

The JSON response:

{
  id: 1,
  name: "Supermarket",
  products: [
    "fruit",
    "eggs"
  ]
}

Here is the corresponding model:

# app/models/shop.rb
class Shop < ActiveRecord::Base
  extend Enumerize
  attr_accessible :name, :products

  serialize :products, Array
  enumerize :products, in: %w{fruit meat eggs}, multiple: true

  resourcify

  validates :name, presence: true, length: { in: 5..50 }    
  validates :products, presence: true
end

I want to use curl to test creating and updating a entry. Therefore, I use the following commands:

Create:

$ curl -X POST http://localhost:3000/api/v1/shops.json -d \
  "shop[name]=Supermarket&shop[products]=fruit,eggs&auth_token=a1b2c3d4"

Update:

$ curl -X PUT http://localhost:3000/api/v1/shops/1.json -d \
  "shop[name]=Supermarket&&shop[products]=fruit,eggs&auth_token=a1b2c3d4"

The value for products need to be submitted as an array. When I run the above commands the following message is returned:

{"errors":{"products":["is invalid"]}

How do I need to write the values of the products array so it works with curl?

解决方案

$ curl -X POST http://localhost:3000/api/v1/shops.json -d \
  "shop[name]=Supermarket \
  &shop[products][]=fruit \
  &shop[products][]=eggs \
  &auth_token=a1b2c3d4"

这篇关于如何通过卷曲后的数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆