2014-02-27 154 views
0

我希望有人可以幫助我如何將以下REST命令轉換爲使用Indy idHttp(或其他組件)的delphi 7。我使用的是parse.com plataform,他們的教程產生捲曲和Python的休息請求,例如:如何在Delphi 7中編寫Rest命令以使用parse.com中的REST API

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 -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:

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 

在捲曲

​​

我的問題是,我怎麼可以這樣做,但在Delphi 7,我希望我的問題是清楚的,因爲我需要這個答案。

+1

可能的重複http://stackoverflow.com/questions/9743591/delphi-rest-api-post-sample –

回答

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 APIWinINet API的HTTP請求。

而我們的新的TDocVariant custom variant type爲簡單的JSON過程,包括後期綁定的屬性名稱。

上面的代碼是恕我直言,很容易遵循,並將從Delphi 6工作到XE5。確保您檢索the latest 1.18 unstable version of our units,因爲TDocVariant剛剛推出。

相關問題