Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Last active December 14, 2015 18:59
Show Gist options
  • Save katsuyoshi/5133202 to your computer and use it in GitHub Desktop.
Save katsuyoshi/5133202 to your computer and use it in GitHub Desktop.
http request にjsonデータを渡すスクリプト
#!/usr/bin/env ruby
# @see: http://stackoverflow.com/questions/5658510/curl-json-post-request-via-terminal-to-a-rails-app
unless ARGV.size == 3
puts <<EOB
Usage: http_req url method data
ex) http_req http://localhost:3000/movies/new.json post "{ movie:{ title:'Star Wars IV'} }"
EOB
exit
end
require 'json'
url = ARGV[0]
method = ARGV[1] || 'get'
data = begin
JSON.generate(eval(ARGV[2]))
rescue
{}
end
system("curl -v -H 'Accept: application/json' -H 'Content-type: application/json' -X #{method.upcase} -d '#{data}' #{url}")
@katsuyoshi
Copy link
Author

post 以外にも対応できる様に変更

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment