如何使用restclient将特定的curl代码转换为ruby代码

时间:2016-04-12 14:37:02

标签: ruby-on-rails curl

API definition我想将curl代码转换为ruby

curl -u "my_username":"my_pass" \
     -X POST \
     -F "positive_examples=@/Users/abc/Downloads/tiger.zip" \
     -F "negative_examples=@/Users/abc/Downloads/leopard.zip" \
     -F "name=tiger" \
     "http://localhost/api/v2/class"

最后,我可以将curl示例转换为Ruby,遵循ruby中的示例:

request = RestClient::Request.new(method: :post,
url: 'http://localhost/api/v2/class',
user: 'my_username', 
password: 'my_pass',
payload: {multipart:true, 
    positive_examples:File.new("/Users/abc/Downloads/tiger.zip", 'rb'),
    negative_examples:File.new("/Users/abc/Downloads/leopard.zip", 'rb')
    name:'tiger'})

1 个答案:

答案 0 :(得分:1)

RestClient::Request.execute method: :post,
                            url: 'http://localhost/api/v2/class',
                            user: 'my_username',
                            password: 'my_pass',
                            payload: {
                              multipart: true,
                              positive_examples: File.new('/Users/abc/Downloads/tiger.zip', 'rb'),
                              negative_examples: File.new('/Users/abc/Downloads/leopard.zip', 'rb'),
                              name: 'tiger',
                            }

只需阅读宝石的README