Search code examples
rubyresthttpsnet-http

convert curl PUT to ruby net:http


I've searched all over the interwebs and read through https://ruby-doc.org/stdlib-2.4.2/libdoc/net/http/rdoc/Net/HTTP.html

I'm still unable to get the right syntax to convert the following curl command to ruby. I'll spare you all the different ways I've tried. Can someone please give me an example of what this should look like in 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

All the examples I see are with POST not PUT


Solution

  • Converted via 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