LoginSignup
4
5

More than 5 years have passed since last update.

API試し方メモ

Posted at

方法1 curl

curl http://localhost:3000/internal/v1/users/partner_organizations?parameter_name=something&another=something_else

パラメタのstringは''で囲まなくてok

パラメタを配列として渡したい場合:
例えばids: [1, 2, 3]をパラメタとして渡したい時
curl http://localhost:3000/internal/v1/users/partner_organizations?いids[]=1&ids[]=2&ids[]=3
一つずつ渡さないといけない...


curl: (3) [globbing] bad range specification in column 136

とか出る時
curl --globoff http://localhost:3000/internal/v1/users/partner_organizations\?user_ids\[\]\=1

--globoffとすれば良い

globoffドキュメント

-g/--globoff
              This  option  switches  off  the "URL globbing parser". When you set this option, you can
              specify URLs that contain the letters {}[] without having them being interpreted by  curl
              itself.  Note  that  these  letters  are not normal legal URL contents but they should be
              encoded according to the URI standard.

方法2 rails cでconsoleを起動しrailsのapiを叩くgem HTTPartyを使う

(アプリケーションでHTTParty gemを使っていることを前提とする)
rails cでconsoleを起動、以下で行ける。
パラメタの渡し方がラク。

url = 'http://localhost:3000/internal/v1/users/partner_organizations'
params = {application_token: 'honya', user_id: [24, 13, 14]}
method = :get

a = JSON.parse(HTTParty.send(method, url, { body: params }).body)

cf) JSON.parseってやるとjson(配列形式)にしてくれる

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5