网站搜索

angular - 从云函数(Firebase)进行 POST x-www urlencoded


我正在尝试从云 Firebase 函数使用 HTTP POST 请求向 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 + '&currency=' + 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.');
}

但是它不起作用,有人能帮我吗

答案#1

我已经设法使用 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;
    });