如何解决使用' net / http'中的HTTP临时重定向错误在红宝石?

时间:2016-06-07 06:26:53

标签: ruby curl http-headers net-http

以下代码不断给出#<Net::HTTPTemporaryRedirect:0x00000001e8cc58>错误。我试着看Ruby - net/http - following redirects,但没有用。

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:如果我删除输出变量并在request["Authorization"]中粘贴命令行输出,我就会成功。奇怪?

1 个答案:

答案 0 :(得分:1)

您是否检查了重定向的位置以及它返回的其他标题?

是否有可能travis token --org包含您必须删除的换行符?

如果这还没有解决,你可以粘贴response.inspect吗?