Ruby send JSON request

后端 未结 10 1324
后悔当初
后悔当初 2020-11-28 20:37

How do I send a JSON request in ruby? I have a JSON object but I dont think I can just do .send. Do I have to have javascript send the form?

Or can I us

10条回答
  •  旧巷少年郎
    2020-11-28 21:19

    The net/http api can be tough to use.

    require "net/http"
    
    uri = URI.parse(uri)
    
    Net::HTTP.new(uri.host, uri.port).start do |client|
      request                 = Net::HTTP::Post.new(uri.path)
      request.body            = "{}"
      request["Content-Type"] = "application/json"
      client.request(request)
    end
    

提交回复
热议问题