Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post array values via curl?

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?

like image 979
JJD Avatar asked Jun 14 '13 11:06

JJD


1 Answers

$ curl -X POST http://localhost:3000/api/v1/shops.json -d \
  "shop[name]=Supermarket \
  &shop[products][]=fruit \
  &shop[products][]=eggs \
  &auth_token=a1b2c3d4"
like image 138
Mike Szyndel Avatar answered Oct 17 '22 20:10

Mike Szyndel