带有API的Ruby传递标头 [英] Ruby pass header with API
问题描述
因此,我一直在尝试为Twitch设置聊天机器人.我希望用户能够输入!stats ,然后他们会看到我的Fortnite统计信息.
So I've been trying to set up my chat bot for Twitch. I want the user to be able to type !stats and then they'll see my Fortnite stats.
他们可以使用命令和内容,这不是我遇到的问题.我在将标头发送到 Fortnite Tracker API 时遇到麻烦.
They can use the command and stuff, that's not what I'm having a problem with. I'm having some trouble sending a header to Fortnite Tracker API.
他们想收到这个消息:TRN-Api-Key: somelong-api-key-right-here
在标题中.
They want to recieve this: TRN-Api-Key: somelong-api-key-right-here
in the header.
我尝试了以下方法: require("httparty")
I've tried these methods: require("httparty")
url = "https://api.fortnitetracker.com/v1/profile/pc/bentearzz"
header = {
key: "TRN-Api-Key: Somelong-api-key-here"
}
response = HTTParty.get(url, header: header)
puts(response.body)
还有这个
require("net/http")
require("json")
url = "https://api.fortnitetracker.com/v1/profile/pc/bentearzz"
uri = URI(url)
response = Net::HTTP::Get.new(uri)
response.basic_auth("TRN-Api-Key: 7af072f0-d195-4c44-b1b4-a8838080e4c4")
JSON.parse(response)
print(response)
请帮助.
推荐答案
您的第一个在正确的轨道上.我认为只有一些语法错误.
Your first one is on the right track. I think there are just a few syntax errors.
url = "https://api.fortnitetracker.com/v1/profile/pc/bentearzz"
headers = {
"TRN-Api-Key": "Somelong-api-key-here"
}
response = HTTParty.get(url, headers: headers)
puts(response.body)
这篇关于带有API的Ruby传递标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!