[英]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.