Search code examples
ruby-on-railscurlrest-client

Using Rest Client to post a curl in rails


I want to traduce this curl into rest client sintax:

curl https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/ag4nktpdzebjiye1tlze/cards \
   -u sk_e568c42a6c384b7ab02cd47d2e407cab: \
   -H "Content-type: application/json" \
   -X POST -d '{
      "token_id":"tokgslwpdcrkhlgxqi9a",
      "device_session_id":"8VIoXj0hN5dswYHQ9X1mVCiB72M7FY9o"
   }' 

The hash I already have it in a variable and the keys or id´s are static so I paste them wherever I need to. This is what I´ve done so far but it doesn't work:

response_hash=RestClient.post "https://sandbox-api.openpay.mx/v1/mdxnu1gfjwib8cmw1c7d/customers/#{current_user.customer_id}/cards \
                                -u sk_083fee2c29d94fad85d92c46cec26b5a:",
                              {params: request_hash}, 
                              content_type: :json, accept: :json

Can someone help me traduce it?


Solution

  • Try this:

    begin
      RestClient.post(
        "https://sk_e568c42a6c384b7ab02cd47d2e407cab:@sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/ag4nktpdzebjiye1tlze/cards",
        { token_id: 'tokgslwpdcrkhlgxqi9a', device_session_id: '8VIoXj0hN5dswYHQ9X1mVCiB72M7FY9o' }.to_json,
        { content_type: :json, accept: :json }
      )
    rescue RestClient::ExceptionWithResponse => e
      # do something with e.response.body
    end