2

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?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.