向 Stripe 发送 JSON 对象的问题
Issue sending a JSON object to Stripe
我正在尝试向 Stripe 发送一个 JSON 对象,但我总是从响应中收到错误消息。
API 已解决但未发送对 /api/ctrlpnl/products_submit 的响应,这可能会导致请求停滞。
{
错误: {
代码:'parameter_unknown',
doc_url: 'https://stripe.com/docs/error-codes/parameter-unknown',
消息:'Received unknown parameter: {"name":"dasdas"}',
参数:'{“名称”:“dasdas”}',
类型:'invalid_request_error'
}
}
我的代码如下:
import Stripe from 'stripe';
import { NextApiRequest, NextApiResponse } from 'next';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27'
});
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
try {
const name = { name: req.body.name };
fetch(`${process.env.BASE_URL}/v1/products`, {
method: 'POST',
body: JSON.stringify(name),
headers: {
'Accept': 'application/json',
"content-type": 'application/x-www-form-urlencoded',
Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
}
}).then((response) => {
return response.json();
}).then(data => {
console.log(data);
res.status(200).json(data)
})
} catch (err) {
res.status(err.statusCode || 500).json(err.message);
}
} else {
res.setHeader('Allow', 'POST');
res.status(405).end('Method Not Allowed');
}
}
fetch
的 content-type
正确设置为 application/x-www-form-urlencoded
,但 body
包含一个 json。所以 Stripe 无法解析 body
参数。
要解决这个问题,您需要将 JSON.stringify
替换为 new URLSearchParams
:
const name = { name: req.body.name };
fetch(`${process.env.BASE_URL}/v1/products`, {
method: 'POST',
body: new URLSearchParams(name), // ← this will return "name=xxxx"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${process.env.STRIPE_SECRET_KEY}`,
}
});
请注意,我建议使用 the Stripe library,它使用起来更简单:stripe.products.create(name);
,特别是因为您已经将其包含在代码中。
我正在尝试向 Stripe 发送一个 JSON 对象,但我总是从响应中收到错误消息。
API 已解决但未发送对 /api/ctrlpnl/products_submit 的响应,这可能会导致请求停滞。 { 错误: { 代码:'parameter_unknown', doc_url: 'https://stripe.com/docs/error-codes/parameter-unknown', 消息:'Received unknown parameter: {"name":"dasdas"}', 参数:'{“名称”:“dasdas”}', 类型:'invalid_request_error' } }
我的代码如下:
import Stripe from 'stripe';
import { NextApiRequest, NextApiResponse } from 'next';
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, {
apiVersion: '2020-08-27'
});
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
try {
const name = { name: req.body.name };
fetch(`${process.env.BASE_URL}/v1/products`, {
method: 'POST',
body: JSON.stringify(name),
headers: {
'Accept': 'application/json',
"content-type": 'application/x-www-form-urlencoded',
Authorization: `Bearer ${process.env.STRIPE_SECRET_KEY}`,
}
}).then((response) => {
return response.json();
}).then(data => {
console.log(data);
res.status(200).json(data)
})
} catch (err) {
res.status(err.statusCode || 500).json(err.message);
}
} else {
res.setHeader('Allow', 'POST');
res.status(405).end('Method Not Allowed');
}
}
fetch
的 content-type
正确设置为 application/x-www-form-urlencoded
,但 body
包含一个 json。所以 Stripe 无法解析 body
参数。
要解决这个问题,您需要将 JSON.stringify
替换为 new URLSearchParams
:
const name = { name: req.body.name };
fetch(`${process.env.BASE_URL}/v1/products`, {
method: 'POST',
body: new URLSearchParams(name), // ← this will return "name=xxxx"
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${process.env.STRIPE_SECRET_KEY}`,
}
});
请注意,我建议使用 the Stripe library,它使用起来更简单:stripe.products.create(name);
,特别是因为您已经将其包含在代码中。