如何在Delphi7中编写Rest命令以使用parse.com中的RESTAPI
我希望有人能帮助我如何将下面的REST命令翻译成Delphi7,以便与Indy idHttp(或其他组件)一起使用。我使用的是parse.com平台,他们的教程使用CURL和Python生成rest请求,例如: 在Python的POST示例中:如何在Delphi7中编写Rest命令以使用parse.com中的RESTAPI,delphi,rest,Delphi,Rest,我希望有人能帮助我如何将下面的REST命令翻译成Delphi7,以便与Indy idHttp(或其他组件)一起使用。我使用的是parse.com平台,他们的教程使用CURL和Python生成rest请求,例如: 在Python的POST示例中: import json,httplib connection = httplib.HTTPSConnection('api.parse.com', 443) connection.connect() connection.request('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中的相同帖子示例:
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
以及如何在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 -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
卷曲:
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 -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
我的问题是,我如何做同样的事情,但在Delphi7中。我希望我的问题很清楚,因为我需要这个答案。一个选择,使用我们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.
它将对HTTP请求使用或
以及我们新的简化JSON过程,包括属性名称的后期绑定
上面的代码非常容易理解,可以从Delphi6一直工作到XE5。确保检索,因为刚刚引入了
TDocVariant
一个选项,使用我们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.
它将对HTTP请求使用或
以及我们新的简化JSON过程,包括属性名称的后期绑定
上面的代码非常容易理解,可以从Delphi6一直工作到XE5。确保检索,因为刚刚引入了TDocVariant
可能的重复可能的重复