0

I'm trying to reproduce a cURL to POST an array of hashes and having trouble with the syntax. This is the basic format I want:

curl -X POST http://localhost:3000/api/v1/shops -d \
  "shop[users][]=1 \
   &shop[users][]=2\
   &shop[users][]=c"

How do I turn the values 1, 2, and c into hashes?

On my Rails API, I want the object to look like this:

{
  shop: {
    users: [
      {
        name: "Foo",
        age: 34
      },
      {
        name: "Bar",
        age: 12
      }
    ]
  }
}

Update

I tried the here-doc solution below but Rails parses that wrong, giving me:

{"{  shop: {    users: "=>{"      {        name: \"Foo\",        age: 34      },      {        name: \"Bar\",        age: 12      }    "=>{"  }}"=>nil}},"action"=>"create", "controller"=>"shops"}

2 Answers 2

1

I was having the same problem and didn't wanted to use a json file, so i find out that this way also works fine! (at least with Rails 3)

curl -i http://localhost:3000/team/999/transfers \
 -X "POST" \
 -d "team[transfers][][player_in_id]=1" \
 -d "team[transfers][][player_out_id]=2" \
 -d "team[transfers][][player_in_id]=3" \
 -d "team[transfers][][player_out_id]=4"
Sign up to request clarification or add additional context in comments.

Comments

0

Try doing this using a here-doc :

$ curl -X POST http://localhost:3000/api/v1/shops -d "@-" <<EOF
{
  shop: {
    users: [
      {
        name: "Foo",
        age: 34
      },
      {
        name: "Bar",
        age: 12
      }
    ]
  }
}
EOF

1 Comment

thanks. Tagging this question with Rails also because Rails does some funky params parsing. Your solution didn't get parsed correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.