Rails: 如何获取带有header的http请求? (产品搜寻 API)
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
。您不需要设置 header 即可;使用 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
。
我正在尝试使用 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
。您不需要设置 header 即可;使用 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
。