[英]using curl via Indy in Delphi XE2
I am trying to use IdHTTP to equivalence this curl operation (known to work) in Delphi XE2: 我正在尝试使用IdHTTP等效于Delphi XE2中的此curl操作(已知有效):
curl http://hub.Healthdata.gov/api/action/datastore_search --data-urlencode '
{
"resource_id": "391792b5-9c0a-48a1-918f-2ee63caa1c54",
"filters": {
"provider_id": 393303
}
}'
I have tried the following code, but it does not work ... can someone advise as to correct procedure? 我已经尝试了以下代码,但无法正常工作……有人可以建议您更正该程序吗? It replies bad request. 它回复了错误的请求。 Thank you. 谢谢。
procedure TfrmMain.get1Click(Sender: TObject);
var
lHTTP: TIdHTTP;
lParamList: TStringList;
result:string;
begin
lParamList := TStringList.Create;
lParamList.Add('"resource_id": "391792b5-9c0a-48a1-918f-2ee63caa1c54"');
lParamList.Add('"filters": {"provider_id": 393303}');
lHTTP := TIdHTTP.Create(nil);
try
Result := lHTTP.Post('http://hub.Healthdata.gov/api/action/datastore_search --data-urlencode ',
lParamList);
finally
FreeAndNil(lHTTP);
FreeAndNil(lParamList);
end;
end;
The --data-urlencode
parameter is not part of the URL, it merely tells curl how to encode the data being posted, so you do not pass that portion to TIdHTTP
at all. --data-urlencode
参数不是URL的一部分,它只是告诉curl如何对发布的数据进行编码,因此您根本不会将该部分传递给TIdHTTP
。
The part of the curl command between single quotes is the actual data to post. 在单引号之间的curl命令的一部分是要发布的实际数据。 --data-urlencode
tells curl to send the data using the HTTP POST
method, using the application/x-www-form-urlencoded
content type, and url-encoding the data. --data-urlencode
告诉curl使用HTTP POST
方法,使用application/x-www-form-urlencoded
内容类型和url编码数据来发送数据。
The TStrings
version of TIdHTTP.Post()
does all of that. TIdHTTP.Post()
的TStrings
版本可以完成所有这些工作。 Normally, application/x-www-form-urlencoded
is used with "name=value" string pairs, however there is no name being specified in the curl command, only a value. 通常, application/x-www-form-urlencoded
与“名称=值”字符串对一起使用,但是在curl命令中没有指定名称,只有一个值。 If curl supplies a default name, then the Delphi code would look like this: 如果curl提供了默认名称,那么Delphi代码将如下所示:
procedure TfrmMain.get1Click(Sender: TObject);
var
json: string;
lHTTP: TIdHTTP;
lParamList: TStringList;
result:string;
begin
json := CRLF +
'{' + CRLF +
' "resource_id": "391792b5-9c0a-48a1-918f-2ee63caa1c54",' + CRLF +
' "filters": {' + CRLF +
' "provider_id": 393303' + CRLF +
' }' + CRLF +
'}';
lParamList := TStringList.Create;
try
lParamList.Add('somename='+json);
lHTTP := TIdHTTP.Create(nil);
try
Result := lHTTP.Post('http://hub.Healthdata.gov/api/action/datastore_search', lParamList);
finally
lHTTP.Free;
end;
finally
lParamList.Free;
end;
end;
Otherwise, if curl sends the specified data as-is by itself, then the Delphi code would look like this instead: 否则,如果curl本身按原样发送指定的数据,则Delphi代码将如下所示:
procedure TfrmMain.get1Click(Sender: TObject);
var
json: string;
lHTTP: TIdHTTP;
lParamList: TStringList;
result:string;
begin
json := CRLF +
'{' + CRLF +
' "resource_id": "391792b5-9c0a-48a1-918f-2ee63caa1c54",' + CRLF +
' "filters": {' + CRLF +
' "provider_id": 393303' + CRLF +
' }' + CRLF +
'}';
lParamList := TStringList.Create;
try
lParamList.Add(json);
lHTTP := TIdHTTP.Create(nil);
try
Result := lHTTP.Post('http://hub.Healthdata.gov/api/action/datastore_search', lParamList);
finally
lHTTP.Free;
end;
finally
lParamList.Free;
end;
end;
The only difference being what parameter value gets passed to TStringList.Add()
. 唯一的区别是将什么参数值传递给TStringList.Add()
。
They are not URL parameters it's part of the POST
body that's just URL encoded. 它们不是URL参数,它只是URL编码的POST
正文的一部分。
You need to encode the JSON string and write it to a stream, perhaps TMemoryStream
and then call: 您需要对JSON字符串进行编码并将其写入流(也许是TMemoryStream
,然后调用:
TIdHTTP.Post(url,stream);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.