我希望有人可以帮助我如何将以下REST命令转换为delphi 7以与Indy idHttp(或其他组件)一起使用。我正在使用parse.com plataform,他们的教程在CURL和Python中生成休息请求,例如:
在Python中的一个POST示例:
import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('POST', '/1/classes/GameScore', json.dumps({
"score": 1337,
"playerName": "Sean Plott",
"cheatMode": False
}), {
"X-Parse-Application-Id": "here-go-application-id",
"X-Parse-REST-API-Key": "here-go-rest-api-key",
"Content-Type": "application/json"
})
result = json.loads(connection.getresponse().read())
print result
这里是CURL中相同的POST示例:
curl -X POST \
-H "X-Parse-Application-Id: here-go-application-id" \
-H "X-Parse-REST-API-Key: here-go-rest-api-key" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
https://api.parse.com/1/classes/GameScore
GET如何在python中请求curl:
python中的:
import json,httplib
connection = httplib.HTTPSConnection('api.parse.com', 443)
connection.connect()
connection.request('GET', '/1/classes/GameScore/Ed1nuqPvcm', '', {
"X-Parse-Application-Id": "here-go-application-id",
"X-Parse-REST-API-Key": "here-go-rest-api-key"
})
result = json.loads(connection.getresponse().read())
print result
CURL中的:
curl -X GET \
-H "X-Parse-Application-Id: here-go-application-id" \
-H "X-Parse-REST-API-Key: here-go-rest-api-key" \
https://api.parse.com/1/classes/GameScore/Ed1nuqPvcm
我的问题是,我怎么能这样做,但是在Delphi 7中。我希望我的问题很明确,因为我需要这个答案。
答案 0 :(得分:1)
使用 mORMot 开源框架的某些部分的一个选项:
uses
SynCrtSock, // for HTTP process
SynCommons; // for TDocVariant type support
var t: variant;
begin
// POST example
// 1. prepare the JSON content (several ways to do the same)
t := _Obj(['score',1337,'playerName','Sean Plott','cheatMode',False]);
// same as:
t := _Json('"score":1337,"playerName":"Sean Plott","cheatMode":False');
// or with MongoDB extended syntax:
t := _Json('score:1337,playerName:"Sean Plott",cheatMode:False');
// or using late-binding to create the object
TDocVariant.New(t);
t.score := 1337;
t.playerName := 'Sean Plott';
t.cheatMode := false;
// 2. create the resource on the server
TWinHTTP.Post( // or TWinINet.Post(
'https://api.parse.com/1/classes/GameScore',t,
'X-Parse-Application-Id: here-go-application-id'#13#10+
'Content-Type: application/json');
// GET example
// 1. retrieve the resource
t := _Json(TWinHTTP.Get('https://api.parse.com/1/classes/GameScore/Ed1nuqPvcm',
'X-Parse-Application-Id: here-go-application-id'#13#10+
'Content-Type: application/json'));
// 2. access the resource members via late-binding of the variant value
writeln('score = ',t.score);
writeln('playerName = ',t.playerName);
writeln('cheatMode = ',t.cheatMode);
end.
它将使用WinHTTP API或WinINet API作为HTTP请求。
我们新的TDocVariant
custom variant type用于简单的JSON过程,包括属性名称的后期绑定。
上面的代码是恕我直言,很容易遵循,并将从Delphi 6到XE5。确保您检索the latest 1.18 unstable version of our units,因为刚刚引入了TDocVariant
。