[英]Rails: How to get a http request with header? (Product Hunt API)
我正在嘗試使用Product Hunt
的API來獲取其每日帖子。 這就是我最后得到的
task :fetch_product_hunt => :environment do
query = "http://api.producthunt.com/v1/posts"
access_token = `My API Key`
response = HTTParty.get(query,
:headers => {'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => access_token,
'Host' => 'api.producthunt.com'}).body
decode_response = ActiveSupport::JSON.decode(response)
puts decode_response
end
問題是我不知道如何為get http request
指定header
。 並且我得到{"status"=>"404", "error"=>"Not Found"}
。
該文檔位於此處: https : //api.producthunt.com/v1/docs/posts/posts_index_get_the_posts_of_today
您實際上已經正確格式化了請求以執行GET
。 您無需設置標題即可; 使用get
方法控制HTTP動詞。
這是證明。 這是您發布的內容的數據包捕獲:
0x0040: d47d 4745 5420 2f76 312f 706f 7374 7320 .}GET./v1/posts.
0x0050: 4854 5450 2f31 2e31 0d0a 4163 6365 7074 HTTP/1.1..Accept
0x0060: 3a20 6170 706c 6963 6174 696f 6e2f 6a73 :.application/js
0x0070: 6f6e 0d0a 436f 6e74 656e 742d 5479 7065 on..Content-Type
0x0080: 3a20 6170 706c 6963 6174 696f 6e2f 6a73 :.application/js
0x0090: 6f6e 0d0a 436f 6e6e 6563 7469 6f6e 3a20 on..Connection:.
0x00a0: 636c 6f73 650d 0a48 6f73 743a 2061 7069 close..Host:.api
0x00b0: 2e70 726f 6475 6374 6875 6e74 2e63 6f6d .producthunt.com
請注意,您可以看到正在使用GET
動詞。 這意味着問題實際上是其他問題。 API文檔不是最新的,或者您的OAUTH令牌有問題。 很難說,因為如果令牌不正確,我會期望使用401
而不是404
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.