【问题标题】:Add JSON to Parse.Cloud.httpRequest "application/x-www-form-urlencoded"将 JSON 添加到 Parse.Cloud.httpRequest "application/x-www-form-urlencoded"
【发布时间】:2016-04-13 10:08:08
【问题描述】:

我一直在尝试将 Stripe 的托管帐户实施到我的云代码功能中。 到目前为止,我设法让它工作,但现在我遇到了一个我似乎无法解决的问题。

归结为: 使用 'content_type': 'application/x-www-form-urlencoded' 如何发送 JSON?

无法将 content_type 更改为 application/JSON,因为 Stripe 需要一个 form-urlencoded。 我试图对 JSON 进行字符串化,但当我这样做时,Stripe 也会抱怨。它需要一个“哈希”,我假设它是一个 JSON。

是否可以对 JSON 进行 url 编码,以便在将 content_type 设置为 form-urlencoded 的同时发送它?

我当前的代码不起作用,因为 Parse 说:

未捕获的错误:无法对对象进行格式编码

var secret_key = stripeKeys.stripeSecretKey;
var cardTokenId = "tok_...";
var country = "BE";
var currency = "EUR";
var email = "test@test.org";
var firstName = "test";
var lastName = "tester";
var dobDay = 1;
var dobMonth = 1;
var dobYear = 1950;
var addressCity = "City";
var addressCountry = "Country";
var addressLine = "Address line";
var addressZIP = "ZIP";
var addressProvince = "Province";       

var createAccountPromise = function()
{
    var params = 
        {
            url: "https://api.stripe.com/v1/accounts",
            method: "POST",
            headers: 
            {
                "Authorization": "Basic " + new Buffer(secret_key + ":").toString("base64"),
                "content_type": "application/x-www-form-urlencoded"
            },
            body: 
            {   
                "country": country,
                "default_currency": currency,
                "email": email,
                "managed": true,
                "legal_entity":
                    {
                        "first_name": firstName,
                        "last_name": lastName,
                        "type": "individual",
                        "dob":
                            {
                                "day": dobDay,
                                "month": dobMonth,
                                "year": dobYear
                            },
                        "personal_address":
                            {
                                "city": addressCity,
                                "country": addressCountry,
                                "line1": addressLine,
                                "postal_code": addressZIP,
                                "state": addressProvince
                            }
                    },
                "external_account": cardTokenId
            }
        };
    return Parse.Cloud.httpRequest(params);
}

createAccountPromise()
    .then(function(result)
    {
        console.log("SUCCESS: " + result.text);
        response.success("Account Created");
    },
    function(errorReason)
    {
        console.log("ERROR: " + errorReason.text);
        response.error("Account NOT Created because: " + errorReason.text);
    });

【问题讨论】:

    标签: javascript json parse-platform stripe-connect


    【解决方案1】:

    问题在于application/x-www-form-urlencoded Content-Type 会导致“平面”key=value 属性列表,而您正在传递具有多个级别的分层对象。 JSON 知道如何编码这样的对象,application/x-www-form-urlencoded 不知道(有几种不同的方法可以做到这一点,使用点符号、括号等)。

    您应该做的是“扁平化”您的 JSON 对象,使其只有一个级别,并使用“扩展”名称作为键。 IE。而不是拥有包含first_namelegal_entity,而是直接设置legal_entity[first_name](以匹配Stripe 使用的格式)。

    所以,你的身体会是:

    body: 
    {   
        "country": country,
        "default_currency": currency,
        "email": email,
        "managed": true,
        "legal_entity[first_name]": firstName,
        "legal_entity[last_name]": lastName,
        "legal_entity[type]": "individual",
        "legal_entity[dob][day]": dobDay,
        "legal_entity[dob][month]": dobMonth,
        "legal_entity[dob][year]": dobYear
        "legal_entity[personal_address][city]": addressCity,
        "legal_entity[personal_address][country]": addressCountry,
        "legal_entity[personal_address][line1]": addressLine,
        "legal_entity[personal_address][postal_code]": addressZIP,
        "legal_entity[personal_address][state]": addressProvince
        "external_account": cardTokenId
    }
    

    当然,如果您有更复杂的对象,可以在代码中“展平”事物,而不是手动进行。

    另外,您应该使用Content-Type,而不是content_type

    【讨论】:

    • 感谢您的解释!它有效 :) 虽然 content_type 或 Content-type 的东西并不重要,但我都试过了,它们都有效
    【解决方案2】:

    使用encodeURIComponent 将 json 转换为 x-www-form-urlencoded。然后发送请求。

    return Parse.Cloud.httpRequest(encodeURIComponent(params));
    

    或者

    return Parse.Cloud.httpRequest(encodeURIComponent(JSON.stringify(params)));
    

    【讨论】:

    • 这给了我错误:无法调用未定义的方法'toUpperCase'
    • 这可能是因为一个或多个 params 对象属性未定义。调用函数时请检查params对象的所有值。
    • 我更新了代码,告诉我你需要什么,我会提供:)
    • 请将此行添加到您的代码中。返回前的 console.log(JSON.stringify(params, null, 4)) Parse.Cloud.httpRequest(params);然后请运行代码并发送输出
    猜你喜欢
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 2012-04-09
    • 2019-10-26
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多