[英]parse “application/x-www-form-urlencoded” response with javascript
[英]Add JSON to Parse.Cloud.httpRequest “application/x-www-form-urlencoded”
我一直在尝试将Stripe的托管帐户实施到我的云代码功能中。 到目前为止,我设法使其工作,但现在我遇到了一个我似乎无法解决的问题。
归结为:使用'content_type': 'application/x-www-form-urlencoded'
如何发送JSON?
由于Stripe期望使用form-urlencoded,因此无法将content_type更改为application/JSON
。 我试图将JSON字符串化,但是当我这样做时,Stripe也会抱怨。 它期望一个'哈希',我假设它是一个JSON。
是否可以url编码JSON,以便我可以发送它,同时仍然将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);
});
问题来自于application/x-www-form-urlencoded
Content-Type导致属性的“flat” key=value
列表,而您传递的是具有多个级别的分层对象。 JSON知道如何对这样的对象进行编码, application/x-www-form-urlencoded
不会(有几种不同的方法,用点表示法,括号等)。
你应该做的是“展平”你的JSON对象,使它只有一个级别,并使用“扩展”名称作为键。 即不是拥有包含first_name
的legal_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
。
使用encodeURIComponent
将json转换为x-www-form-urlencoded。 然后发送请求。
return Parse.Cloud.httpRequest(encodeURIComponent(params));
要么
return Parse.Cloud.httpRequest(encodeURIComponent(JSON.stringify(params)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.