Search code examples
ruby-on-railsgethttpi

"you are being redirected" status code 302 rails


I have an API which when returns me text/css which is in string format instead of JSON, so when I hit that API using Postman I get back the whole css data with status code 200 but when I hit the same API using HTTPI or any other library I am getting status code 302 with "You are being redirected" with redirected being a hyperlink which points to the correct css data.

Here is how I am using HTTPI to make the get request :

url = "https://<some-end-point>"
request = HTTPI::Request.new
request.url = url
request.headers = {"Content-Type"=>"text/css","Authorization"=>"
<authorised token>","api_version"=>"1"}
response = HTTPI.get(request)
puts response

Solution

  • After a lot of effort, I found out that redirection wasn't supported with HTTPI. So to resolve it I used

    open-uri

    which even handles redirection.

    require 'open-uri'
    
    url = "https://<some-end-point>"
    headers = {"Content-Type"=>"text/css","Authorization"=>"<authorised token>","api_version"=>"1"}
    buffer = open(url,header).read