java developer
java developer

Reputation: 25

Convert http to https using ruby

I want to convert http to https URL in Ruby.

Let me explain how my method works:

The issue which I'm facing now is when I try to do get operation using http, I'm getting 302 Found stating that The document has moved https:devalphaserver.com/xxx/12 here.

Please help. Thanks!

Upvotes: 1

Views: 2356

Answers (2)

Hosam Aly
Hosam Aly

Reputation: 42453

url = "http:devalphaserver.com/xxx/12"
http_uri = URI(url)
https_uri = URI(http_uri.tap { |u| u.scheme = 'https' }.to_s)

Upvotes: 0

shivam
shivam

Reputation: 16506

Convert http to https using Ruby

url = "http:devalphaserver.com/xxx/12"
uri = URI.parse(url)
uri.scheme = "https"
uri.to_s
# => "https:devalphaserver.com/xxx/12"

However going through your question, I can see its a case of 302 redirect. If you are using gems like REST Client or HTTParty they have in place the mechanism of result handling that takes care of url redirect automatically.

Upvotes: 10

Related Questions