首页 > 解决方案 > 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 + '&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.');
}

但它不起作用任何人都可以帮助我

标签: angulartypescriptfirebasegoogle-cloud-functions

解决方案


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

推荐阅读