-1

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?

1 Answers1

1

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
mikwat
  • 533
  • 3
  • 11
  • Thank you very much @mikwat it worked perfectly, my only problem though is that when I make the post incorrectly the api responds with de error description, but restclient doesn't lets me receive it. It just posts an error from the server, it displays 401 error bad payment request, but I don't want it to crash, I want to reveice the error json from the api and read it. Do you know how to do that @mikwat? – Victor Elizalde May 02 '17 at 18:18
  • I've updated my solution to include error handling. See more info here: https://github.com/rest-client/rest-client#exceptions-see-httpwwww3orgprotocolsrfc2616rfc2616-sec10html – mikwat May 02 '17 at 18:26
  • I think you could do something like `JSON.parse(e.response.body)['description']`. – mikwat May 02 '17 at 20:55