25

I've got an iPhone app requesting an API, and it uses a nested param structure to pass in a username and password to login.

In my Rails controller, I'm successfully retrieving this information normally using:

username = param[:session][:username]

I'd like to test my API from shell, using cURL. But I can't figure out how to provide nested params? The code below does NOT work... And I've tried multiple variations. But just can't seem to figure it out?

curl -d '{"session":{"username":"test","password":"password"}}' http://testurl/remote/v1/sessions

Any help, much appreciated!

boymc
  • 1,217
  • 2
  • 13
  • 17

2 Answers2

62

Different frameworks may interpret http query params differently, but for rails you should be able to get away with the following:

curl -d 'session[username]=name&session[password]=pwd' http://testurl/remote/v1/sessions
Joseph Rodriguez
  • 1,135
  • 9
  • 14
0

I found this to work. Given a JSON blob like

{ "opts": {
    "finder": { 
      "cname":"superlatives"
    }
  }
}



curl -H "Content-Type: application/json" \
  -X POST \
  --data-binary '{"opts":{"finder":{"cname":"superlatives"}}}' \
  http://YOURSERVER.com/api/grams/one
dcsan
  • 11,333
  • 15
  • 77
  • 118
  • note that just `--data` strips CR and LF so you could use that too if you want to pretty print your JSON – dcsan Nov 06 '18 at 09:51