How to invoke HTTP POST method over SSL in ruby?

前端 未结 4 1425
深忆病人
深忆病人 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:50

    There's a very good explanation of how to make a JSON POST request with Net::HTTP at this link.

    I would recommend using a library like HTTParty. It's well-documented, you can just set up your class like so:

    class RackSpaceClient
      include HTTParty
    
      base_uri "https://auth.api.rackspacecloud.com/"
      format :json
      headers 'Accept' => 'application/json'
    
      #methods to do whatever
    
    end
    

    It looks like the main difference between the Ruby code you placed there, and the curl request, is that the curl request is POSTing JSON (content-type application/json) to the endpoint, whereas request.set_form_data is going to send a form in the body of the POST request (content-type application/x-www-form-urlencoded). You have to make sure the content going both ways is of type application/json.

提交回复
热议问题