Axios条纹问题。“ invalid_request_error”

arc*_*ryx 0 javascript stripe-payments axios

我不知道该怎么做!我正在尝试通过Stripe API创建客户。用他们的例子卷曲我没有问题。这是他们的例子:

curl https://api.stripe.com/v1/customers \ -u sk_test_apikey: \ -d description="Customer for zoey.brown@example.com" \ -d source=tok_visa

当我尝试使用axios执行此操作时,出现错误“ invalid_request_error”,因为它无法正确解析我的数据。这是我所拥有的:

export const registerNewUser = async (firstName, lastName, email, password) => {
  let config = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': `Bearer ${stripeTestApiKey}`
    }
  }
  let data = {
      email: `${email}`,
      description: `Customer account for ${email}`
  }
  await axios.post(stripeCustomerUri, data, config)
    .then(res => {
      console.log("DEBUG-axios.post--res: ", res)
    })
    .catch(err => {
      console.log(JSON.stringify(err, null, 2))
    })
}
Run Code Online (Sandbox Code Playgroud)

在我的控制台中,我看到条带没有以正确的方式接收我的数据。这是(我的有用部分)响应json:

"response": { 
  "data": { 
    "error": { 
      "type": "invalid_request_error", 
      "message": "Received unknown parameter: {\"email\":\"joe@blow.com\",\"description\":\"Customer account for joe@blow.com\"}", "param": "{\"email\":\"joe@blow.com\",\"description\":\"Customer account for joe@blow.com\"}" } },
Run Code Online (Sandbox Code Playgroud)

所有我的其他的尝试来看,这典型的错误,我没有通过我的数据在正确的格式。但是,当我通过-d我的curl命令一切正常......如果我发送一个空字符串作为data它的工作原理还有...

有谁知道为什么/这是怎么回事?通过curl的“数据”对象与我的javascript数据对象有何不同?

arc*_*ryx 6

问题是axios默认情况下使用application / json内容类型,并且条带化api需要以form-url编码...这需要在传递给条带化api之前使用诸如querystring之类的库来解析数据对象...希望这会有所帮助有人!