Axios 条带问题。 "invalid_request_error"

Axios stripe issues. "invalid_request_error"

我已经无计可施了!我正在尝试通过条纹 api 创建客户。将他们的示例与 curl 一起使用我没有问题。 这是他们的例子:

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))
    })
}

在我的控制台中,我看到 stripe 没有以正确的方式接收我的数据。这是(我有用的部分)回复 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\"}" } },

从我所有其他尝试和这个示例错误来看,我没有以正确的格式传递我的数据......但是,当我将 -d 传递给我的 curl 命令时,一切都按预期工作...... . 如果我发送一个空字符串作为 data 它也能工作...

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

问题是 axios 默认使用 application/json 内容类型,条带 api 需要 form-url-encoded...这需要用库解析数据对象在传递到条带之前喜欢查询字符串 api...希望这对某人有帮助!