Last active
January 4, 2016 08:09
-
-
Save kimihito/8593797 to your computer and use it in GitHub Desktop.
2014/01/24教えてもらいました。Railsのparamsの部分
This file contains 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
require 'uri' | |
require 'net/http' | |
require 'json' | |
puts 'post data' | |
puts 'url parsed.' | |
urls = ['http://www.youtube.com/watch?v=webZ80CGezY', 'http://www.youtube.com/watch?v=wCnLeg04LEU','http://www.youtube.com/watch?v=WXUEeecBcsU','http://www.youtube.com/watch?v=75fXFouP8mo' ] | |
data = { | |
data: | |
{ | |
name: 'kimihito_', | |
image_url: 'https://pbs.twimg.com/profile_images/426273251476926464/rAc5cOcZ.jpeg', | |
text: 'hoge', | |
urls: urls | |
} | |
} | |
uri = URI.parse('http://0.0.0.0:3000/create') | |
post uri, data | |
def post url, hash | |
req = Net::HTTP::Post.new url | |
req.body = hash.to_json | |
req.content_type = 'application/json' | |
Net::HTTP::start(url.hostname, url.port) {|http|http.request(req)} | |
end | |
#Railsの受け取り側のControllerでの処理。JSON.parseで指定してから取り出す。 | |
data = JSON.parse(params[:data]) | |
name = data[:name] | |
img_url = data[:image_url] | |
text = data[:text] | |
urls = data[:urls] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment