Ruby pass header with API

2019-07-26 23:33发布

问题:

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.

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.

They want to recieve this: TRN-Api-Key: somelong-api-key-right-here in the header.

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)

and this

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)

Please help.

回答1:

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)


标签: json ruby api http