Reputation: 1608
I have my API setup like this:
url = URI.parse('https://www.reddit.com/search.json?q=' + @query + '&limit=' + @results)
response = Net::HTTP.get_response(URI(url))
now I get returned a HTTPTooManyRequest
which is because Reddit blocks generic headers. So I am trying to modify my header, but I'm unable to find a way to add a header to a get_response
method.
How could I add a User-agent header to this?
Upvotes: 2
Views: 729
Reputation: 11813
User alternative syntax:
uri = URI('https://www.reddit.com/search.json')
uri.query = URI.encode_www_form(q: @query, limit: @results)
request = Net::HTTP::Get.new(uri)
request['User-Agent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
response = Net::HTTP.start(uri.hostname) do |http|
http.request(req)
end
Upvotes: 2