Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted
$ curl -X POST http://localhost:3000/api/v1/shops.json -d \
  "shop[name]=Supermarket \
  &shop[products][]=fruit \
  &shop[products][]=eggs \
  &auth_token=a1b2c3d4"
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.