angular - POST x-www urlencoded 从云函数(Firebase)
问题描述
我正在尝试使用 HTTP POST 请求从云 Firebase 函数向 Stripe API 发出 API 请求。
必须传递的参数必须是x-www urlencoded
格式。
const httpOptions = {
headers: new Headers({
Authorization: 'Bearer sk_test_***',
'Content-Type': 'application/x-www-form-urlencoded'
})
};
const params = 'amount=' + payment_intent.amount + '¤cy=' + payment_intent.currency;
const CHARGE_URL = 'https://api.stripe.com/v1/payment_intents';
try {
const snapshot: any = await Http.post(CHARGE_URL, params, httpOptions).toPromise();
const intent: any = {
id: snapshot.id,
client_secret: snapshot.client_secret
};
await customerClassService.savePaymentIntent(requestId, intent);
resp.status(200)
.send(await Promise.all(intent));
} catch (e) {
console.error(e);
resp.status(400)
.send('An error occurred and will be solved ASAP.');
}
但它不起作用任何人都可以帮助我
解决方案
我已经设法使用request-promise
库https://www.npmjs.com/package/request-promise让我的工作
const request = require('request-promise-native');
request.post(uri, {
headers: {
"content-type": "application/x-www-form-urlencoded"
},
form: {
"param1": "param1",
"param2": "param2"
}
})
.catch(err => {
console.log(err);
})
.then(body =>{
return body;
});
推荐阅读
- django - 如何在 django 中创建一个具有多个模型之一的外键的模型?
- jquery - 在悬停时显示元素延迟
- java - 如何在另一个应用程序模块中使用项目源代码
- sql - 在 UPDATE 查询中使用 IN 子句的语法错误
- javascript - Mongoose connectio occured MongoNetworkError: connection 0 to cluster0-shard-00-01-utzss.mongodb.net:27017 closed error
- core-audio - Core Audio MIDI Synth AU & MusicPlayer - Cannot be pulled by AudioUnitRender programmatically
- scala - Why the code is not producing the output as it is supposed to be in the logic implemented?
- firebase - Flutter Retrieve Data from Firebase
- opencv - opencv resize window for a live cam
- javascript - please tell me how to write code for chrome extension to be in full screen