从rails控制器提交POST请求

时间:2014-11-07 06:20:15

标签: ruby-on-rails http-post

我已经按照this question.的回答了。这就是我所拥有的。

  def index
       $response = get_insta_feed
  end

  def get_insta_feed
    require "net/http"
    require 'net/https'
    require "uri"

    $tag = "test"
    uri = URI.parse("https://api.instagram.com/v1/tags/"+$tag+"/media/recent")
    uri.query = URI.encode_www_form(parameters)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Post.new(uri.request_uri)
    http.request(request).to_json
  end

  private

  def parameters
    {
        "access_token" => "my access token here"
    }
  end

在我的视图页面上我只想在解析我想要显示的数据之前先显示完整的json响应,这样:

<div>
  <%=$response%>
</div>

这就是我的div中显示的内容:

{"server":["nginx"],"date":["Fri, 07 Nov 2014 06:13:34 GMT"],"content-type":["text/html; charset=utf-8"],"allow":["GET"],"content-language":["en"],"expires":["Sat, 01 Jan 2000 00:00:00 GMT"],"vary":["Cookie, Accept-Language"],"pragma":["no-cache"],"cache-control":["private, no-cache, no-store, must-revalidate"],"set-cookie":["csrftoken=e70981e518d478dd7362049f9ce89cc9; expires=Fri, 06-Nov-2015 06:13:34 GMT; Max-Age=31449600; Path=/","ccode=PH; Path=/"],"connection":["close"]}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

请使用 instagram gem来完成与Instagram的连接任务。例如,您可以按如下方式使用它:

client = Instagram.client(:access_token => session[:access_token])
for media_item in client.tag_recent_media(tag_name)
   # Use the folowing fields... 
   # media_item.images.thumbnail.url
   # media_item.id
   # media_item.likes[:count]
end

有关gem的更多信息,请参阅its github page