How to invoke HTTP POST method over SSL in ruby?

前端 未结 4 1433
深忆病人
深忆病人 2020-12-02 16:56

So here\'s the request using curl:

curl -XPOST -H content-type:application/json -d \"{\\\"credentials\\\":{\\\"username\\\":\\\"username\\\",\\\"key\\\":\\\"         


        
4条回答
  •  天命终不由人
    2020-12-02 17:42

    You are close, but not quite there. Try something like this instead:

    uri = URI.parse("https://auth.api.rackspacecloud.com")
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Post.new("/v1.1/auth")
    request.add_field('Content-Type', 'application/json')
    request.body = {'credentials' => {'username' => 'username', 'key' => 'key'}}.to_json
    response = http.request(request)
    

    This will set the Content-Type header as well as post the JSON in the body, rather than in the form data as your code had it. With the sample credentials, it still fails, but I suspect it should work with real data in there.

提交回复
热议问题