Equivalent of cURL for Ruby?

前端 未结 12 2598
不知归路
不知归路 2020-11-29 00:33

Is there a cURL library for Ruby?

相关标签:
12条回答
  • 2020-11-29 01:13

    Curb-fu is a wrapper around Curb which in turn uses libcurl. What does Curb-fu offer over Curb? Just a lot of syntactic sugar - but that can be often what you need.

    0 讨论(0)
  • 2020-11-29 01:17

    You might also have a look at Rest-Client

    0 讨论(0)
  • 2020-11-29 01:18

    Here's a little program I wrote to get some files with.

    base = "http://media.pragprog.com/titles/ruby3/code/samples/tutthreads_"
    
    for i in 1..50
    
      url = "#{ base }#{ i }.rb"
      file = "tutthreads_#{i}.rb"
    
      File.open(file, 'w') do |f|   
        system "curl -o #{f.path} #{url}"
      end
    
    end
    

    I know it could be a little more eloquent but it serves it purpose. Check it out. I just cobbled it together today because I got tired of going to each URL to get the code for the book that was not included in the source download.

    0 讨论(0)
  • 2020-11-29 01:22

    Use OpenURI and

      open("http://...", :http_basic_authentication=>[user, password])
    

    accessing sites/pages/resources that require HTTP authentication.

    0 讨论(0)
  • 2020-11-29 01:24

    the eat gem is a "replacement" for OpenURI:

    # first do gem install eat
    require 'eat'
    eat('http://yahoo.com')                 #=> String
    eat('/home/seamus/foo.txt')             #=> String
    eat('file:///home/seamus/foo.txt')      #=> String
    

    It uses HTTPClient under the hood. It also has some options:

    eat('http://yahoo.com', :timeout => 10)                   # timeout after 10 seconds
    eat('http://yahoo.com', :limit => 1024)                   # only read the first 1024 chars
    eat('https://yahoo.com', :openssl_verify_mode => 'none')  # don't bother verifying SSL certificate
    
    0 讨论(0)
  • 2020-11-29 01:25

    To state the maybe-too-obvious, tick marks execute shell code in Ruby as well. Provided your Ruby code is running in a shell that has curl:

    puts `curl http://www.google.com?q=hello`
    

    or

    result = `
      curl -X POST https://www.myurl.com/users \
      -d "name=pat" \
      -d "age=21"
    ` 
    puts result
    
    0 讨论(0)
提交回复
热议问题