How do I make a POST request with open-uri?

后端 未结 4 1759
一向
一向 2020-12-08 07:27

Is it possible to make a POST request from Ruby with open-uri?

4条回答
  •  再見小時候
    2020-12-08 08:03

    As simple as it gets:

    require 'open-uri'
    require 'net/http'
    
    response = Net::HTTP.post_form(URI.parse("https://httpbin.org/post"), { a: 1 })
    
    puts response.code
    puts response.message
    puts response.body
    

    I recommend using response.methods - Object.methods to see all the available methods, e.g. message, header,

    Bonus: POST / DELETE requests:

    puts Net::HTTP.new("httpbin.org").post("/post", "a=1").body
    puts Net::HTTP.new("httpbin.org").delete("/delete").body
    

提交回复
热议问题