Last active
December 14, 2015 18:59
-
-
Save katsuyoshi/5133202 to your computer and use it in GitHub Desktop.
http request にjsonデータを渡すスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
post 以外にも対応できる様に変更