将curl PUT转换为ruby net:http

时间:2018-05-30 18:38:01

标签: ruby rest https net-http

我搜遍了所有的互联网,并通读了https://ruby-doc.org/stdlib-2.4.2/libdoc/net/http/rdoc/Net/HTTP.html

我仍然无法使用正确的语法将以下curl命令转换为ruby。我会饶恕你尝试过的所有不同方式。有人可以给我一个关于Ruby应该是什么样子的例子吗?

curl -k -s -H "Accept: application/json" -H "Content-Type:application/json" -X PUT --data '{"name": "anonymous", "password": "obfuscated"}' https://some.domain/repository/npm/-/user/org.couchdb.user:anonymous

我看到的所有例子都是POST而不是PUT

1 个答案:

答案 0 :(得分:0)

通过Postman转换。

require 'uri'
require 'net/http'

url = URI("https://some.domain/repository/npm/-/user/org.couchdb.user:anonymous")

http = Net::HTTP.new(url.host, url.port)

request = Net::HTTP::Put.new(url)
request["Accept"] = 'application/json'
request["Content-Type"] = 'application/json'
request["Cache-Control"] = 'no-cache'
request["Postman-Token"] = '9e3622cd-fda5-458d-2521-325849546ef7'
request.body = "{\"name\": \"anonymous\", \"password\": \"obfuscated\"}"

response = http.request(request)
puts response.read_body