【问题标题】:POST x-www urlencoded from cloud function (Firebase)POST x-www urlencoded 从云函数(Firebase)
【发布时间】:2020-03-13 01:12:15
【问题描述】:

我正在尝试使用 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.');
}

但是没有用,谁能帮帮我

【问题讨论】:

  • 你能告诉我你正在使用的角度版本吗?
  • 它是firebase的云功能所以节点8作为引擎“typescript”:“^3.2.2”
  • 我遇到了同样的问题。在 Vue 中,我将使用 post 参数实例化一个 FomData 类型,然后将其放入请求的 body 中。但是打字稿似乎无法识别FormData 类型。

标签: angular typescript firebase google-cloud-functions


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-08-18
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2012-07-02
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多