如何从 rails 应用程序执行 curl

How to execute curl from rails application

您好,我正在尝试为我的 rails 应用程序创建一个支付模块。这是他们提供的其余部分 api 我尝试使用 RestClient 但它正在返回 400 个错误请求。

curl -X POST \
  https://api.sumup.com/token \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'grant_type=client_credentials'\
  -d 'client_id=**Client_ID**'\
  -d 'client_secret=**Client_Secret**'

这是我的 restclient 方法的样子:

RestClient::Request.execute(
method: :post, 
url: "https://api.sumup.com/token",
data: "grant_type=client_credentials&client_id=**CLIENT_ID**&client_secret=**Client_Secret**",
headers: { "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencode" }
)

我是不是做错了什么?

您不需要手动对参数进行编码,这很可能是错误的来源。

RestClient.post(
  "https://api.sumup.com/token",
  {
     grant_type: "client_credentials"
     client_id: "**CLIENT_ID**"
     client_secret: "**Client_Secret**"
  },
  { 
    accept: "application/json", 
    content_type: "application/x-www-form-urlencode" 
  }
)