JSON 未收到使用 Indy 发送的内容,因为它是由 Stripe 发送的 API
JSON sent with Indy is not received as it is sent by Stripe API
我正在向条带 API 发送带有 Indy http 组件的 JSON,但 API 没有收到它,因为它应该在我收到“错误请求”响应:
jsnObj := TJSONObject.Create;
jsnObj.AddPair('amount', TJSONNumber.Create('111'));
jsnObj.AddPair('currency', 'eur');
jsnObj.AddPair('customer', 'cus_JNxQsqf6BoK8Rt');
jsnObj.AddPair('description', 'My First Test');
ss := TStringStream.Create(jsnObj.ToString, TEncoding.UTF8);
rs := TStringStream.Create;
IdHTTP1.Request.BasicAuthentication := True;
IdHTTP1.Request.Username := ApiKey ; // test private key
IdHTTP1.Post('https://api.stripe.com/v1/charges', ss, rs);
StatusBar1.SimpleText := IdHTTP1.ResponseText;
要发送的 JSON 是:
{
"amount": 111,
"currency": "eur",
"customer": "cus_JNxQsqf6BoK8Rt",
"description": "My First Test"
}
API 仪表板报告已收到:
{
"{"amount":111,"currency":"eur","customer":"cus_JNxQsqf6BoK8Rt","description":"My First Test"}": null
}
HTTP 组件应该做一些事情以便以这种方式发送,包括空值,可能是因为在请求中包含用户名?对于其他 APIs,同一个 HTTP 组件总是发送它应该发送的内容。 Stripe 支持表明问题出在我这边。
条带文档指定了这一点:
curl -X POST https://api.stripe.com/v1/charges \
-u STRIPE_SECRET_KEY: \
-d amount=2000 \
-d currency=usd \
-d source=tok_visa \
-d description="Charge for aiden.jones@example.com"
有人知道问题出在哪里吗?
Stripe 文档中提供的 CURL 示例根本没有以 JSON 格式发送数据。根据 CURL documentation:
,它以 application/x-www-form-urlencoded
格式发送 name=value
对
-d, --data
(HTTP MQTT) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.
在发布 application/x-www-form-urlencoded
请求时使用 TIdHTTP.Post()
的 TStrings
重载,例如:
var
postData: TStringList;
rs: string;
...
postData := TStringList.Create;
try
postData.Add('amount=111');
postData.Add('currency=eur');
postData.Add('customer=cus_JNxQsqf6BoK8Rt');
postData.Add('description=My First Test');
IdHTTP1.Request.BasicAuthentication := True;
IdHTTP1.Request.Username := ApiKey; // test private key
rs := IdHTTP1.Post('https://api.stripe.com/v1/charges', postData);
StatusBar1.SimpleText := IdHTTP1.ResponseText;
finally
postData.Free;
end;
我正在向条带 API 发送带有 Indy http 组件的 JSON,但 API 没有收到它,因为它应该在我收到“错误请求”响应:
jsnObj := TJSONObject.Create;
jsnObj.AddPair('amount', TJSONNumber.Create('111'));
jsnObj.AddPair('currency', 'eur');
jsnObj.AddPair('customer', 'cus_JNxQsqf6BoK8Rt');
jsnObj.AddPair('description', 'My First Test');
ss := TStringStream.Create(jsnObj.ToString, TEncoding.UTF8);
rs := TStringStream.Create;
IdHTTP1.Request.BasicAuthentication := True;
IdHTTP1.Request.Username := ApiKey ; // test private key
IdHTTP1.Post('https://api.stripe.com/v1/charges', ss, rs);
StatusBar1.SimpleText := IdHTTP1.ResponseText;
要发送的 JSON 是:
{
"amount": 111,
"currency": "eur",
"customer": "cus_JNxQsqf6BoK8Rt",
"description": "My First Test"
}
API 仪表板报告已收到:
{
"{"amount":111,"currency":"eur","customer":"cus_JNxQsqf6BoK8Rt","description":"My First Test"}": null
}
HTTP 组件应该做一些事情以便以这种方式发送,包括空值,可能是因为在请求中包含用户名?对于其他 APIs,同一个 HTTP 组件总是发送它应该发送的内容。 Stripe 支持表明问题出在我这边。 条带文档指定了这一点:
curl -X POST https://api.stripe.com/v1/charges \
-u STRIPE_SECRET_KEY: \
-d amount=2000 \
-d currency=usd \
-d source=tok_visa \
-d description="Charge for aiden.jones@example.com"
有人知道问题出在哪里吗?
Stripe 文档中提供的 CURL 示例根本没有以 JSON 格式发送数据。根据 CURL documentation:
,它以application/x-www-form-urlencoded
格式发送 name=value
对
-d, --data
(HTTP MQTT) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.
在发布 application/x-www-form-urlencoded
请求时使用 TIdHTTP.Post()
的 TStrings
重载,例如:
var
postData: TStringList;
rs: string;
...
postData := TStringList.Create;
try
postData.Add('amount=111');
postData.Add('currency=eur');
postData.Add('customer=cus_JNxQsqf6BoK8Rt');
postData.Add('description=My First Test');
IdHTTP1.Request.BasicAuthentication := True;
IdHTTP1.Request.Username := ApiKey; // test private key
rs := IdHTTP1.Post('https://api.stripe.com/v1/charges', postData);
StatusBar1.SimpleText := IdHTTP1.ResponseText;
finally
postData.Free;
end;