2

I am trying to use a code that I used to use with another Mac.

now when I run it with a new Mac (2018) I get the following error

This is the code

require 'net/http'

base = 'www.uniprot.org'
tool = 'uploadlists'
params = {
  'from' => 'ACC', 'to' => 'P_REFSEQ_AC', 'format' => 'tab',
  'query' => 'P13368 P20806 Q9UM73 P97793 Q17192'
}

http = Net::HTTP.new base
$stderr.puts "Submitting...\n";
response = http.request_post '/' + tool + '/',
  params.keys.map {|key| key + '=' + params[key]}.join('&')

loc = nil
while response.code == '302'
  loc = response['Location']
  response = http.request_get loc
end

while loc
  wait = response['Retry-After'] or break
  $stderr.puts "Waiting (#{wait})...\n";
  sleep wait.to_i
  response = http.request_get loc
end

response.value # raises http error if not 2xx
puts response.body

and this is the error I get

/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb:120:in `error!': 301 "Moved Permanently" (Net::HTTPRetriableError)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/net/http/response.rb:129:in `value'
    from conver.rb:28:in `<main>'
Learner
  • 757
  • 3
  • 15

1 Answers1

1

You're receiving an HTTP 301 Moved Permanently response code. When you look closer you can see it's pointing you to https://www.uniprot.org:443/uploadlists/ for the new location; this is what typically happens when a resource that was previously reachable via HTTP is now only reachable via HTTPS. It doesn't have anything to do with using a new computer; it's just coincidental that it happened around the same time.

If you change the URL to HTTPS it should work as you expect. That said, I never encourage the use of Net::HTTP directly because it's clunky. Take a look at how awkward it is just to make a connection using HTTPS! It's not worth the headache.

I prefer to use HTTParty because it's straightforward and easy to use, as well as being very popular in the Ruby community. Here's an example of how to accomplish your task with HTTParty in fewer lines of code:

require 'httparty'

params = {
  'from' => 'ACC', 'to' => 'P_REFSEQ_AC', 'format' => 'tab',
  'query' => 'P13368 P20806 Q9UM73 P97793 Q17192'
}

response = HTTParty.post(
  'https://www.uniprot.org:443/uploadlists/',
  {
    body: params.keys.map { |key| key + '=' + params[key] }.join('&'),
    headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
  }
)

Then you can inspect the response body:

puts response.body
From    To
P13368  NP_511114.2
Q9UM73  NP_004295.2
P97793  NP_031465.2
Q17192  XP_004934106.1

Additionally, I can tell from the stack trace that you posted that you're using the system version of Ruby that came with macOS. My advice is: Don't use system Ruby.

Instead, you should install a Ruby manager like RVM:

  • Install RVM with \curl -sSL https://get.rvm.io | bash -s stable
  • Reload your shell
  • Install Ruby with rvm install 2.5.3
  • Reinstall your gems (gem install httparty)

Then you can re-run your application.

anothermh
  • 9,815
  • 3
  • 33
  • 52
  • can you explain the loop you made and you remove the part of the code I did ? I just want to learn your approach – Learner Jan 09 '19 at 16:01
  • I do not understand your question. I didn’t write a loop, and if I remove the part of the code that you wrote then it won’t work anymore. Can you rephrase your question to be more clear? – anothermh Jan 09 '19 at 19:22