Search code examples
rubycurlhttp-headersnet-http

How to solve HTTP Temporary Redirect error in using 'net/http' in ruby?


This following code continuously gives me #<Net::HTTPTemporaryRedirect:0x00000001e8cc58> error. I tried to look at Ruby - net/http - following redirects, but of no use.

require 'uri'
require 'net/http'
require 'json'
uri = URI.parse("https://api.travis-ci.org/repo/shahsaurabh0605%2FRuby-Docker/requests")
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
output = `travis token --org`
request = Net::HTTP::Post.new(uri.path)
request["Content-Type"] = 'application/json'
request["Accept"] = 'application/json'
request["Travis-API-Version"] = '3'
request["Authorization"] = 'token '+output
request.body = {'request' => {'branch' => 'master'}}.to_json
response = https.request(request)
puts response

PS: If I remove the output variable and just paste the command line output in request["Authorization"] I get a success. Strange?


Solution

  • Have you checked where it redirects, and which other headers it returns?

    Is it possible, that travis token --org includes a newline that you have to strip?

    If that doesn't solve it, could you paste the response.inspect ?