在 rails 中使用 Rest Client post 卷曲

Using Rest Client to post a curl in rails

我想将这个 curl 转化为 rest client 语法:

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"
   }' 

散列我已经在变量中了,键或 ID 是静态的,所以我将它们粘贴到任何需要的地方。这是我到目前为止所做的,但它不起作用:

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

谁能帮我翻译一下?

试试这个:

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