2014-06-19 22 views
2

我試圖發送HTTP請求來利用DELPHI XE5 UPDATE2的REST客戶端庫組件的谷歌日曆/ v3的API(TRESTClientTRESTRequestTRESToAuth2AutenticatorTRESTResponse) 。在Delphi IDE中設置這些組件後,GET請求運行良好,返回所需的JSON響應。發送POST HTTP請求發送到谷歌日曆/ V3 API返回解析錯誤

但是POST請求(https://www.googleapis.com/calendar/v3/calendars/primary/events)在TRESTRequest.params參數與選項poDoNotEncode和值設置爲pkREQUESTBODY

{ 
    "end":{ 
     "date":"2014-06-13" 
    }, 
    "start":{ 
     "date":"2014-06-13" 
    }, 
    "summary":"reTest" 
} 

返回谷歌API錯誤:

{ 
    "error":{ 
     "errors":[ 
     { 
      "domain":"global", 
      "reason":"parseError", 
      "message":"This API does not support parsing form-encoded input." 
     } 
     ], 
     "code":400, 
     "message":"This API does not support parsing form-encoded input." 
    } 
} 

此代碼和參數在DELPHI XE6中運行良好,而不是在XE5中。

procedure TForm2.Button1Click(Sender: TObject); 
var 
    rBody: TStringStream; 
begin 
    rBody := TStringStream.Create('{"end": {"date": "2014-06-13"},"start": {"date": "2014-06-13"},"summary": "reTest"}'); 
    RESTRequest.AddBody(rBody, ctAPPLICATION_JSON); 
    RESTRequest.Execute; 
end; 

是否有someting我做錯了,我該怎麼辦才能讓請求工作?謝謝你的幫助。

回答

0

基於API描述在

https://developers.google.com/google-apps/calendar/v3/reference/events/insert

它看起來像JSON部分屬於在POST請求體。

錯誤消息聽起來像JSON部分被編碼爲HTML表單字段值(您可以使用像Fiddler2這樣的HTTP代理進行驗證)。

提示:如果您有一個基於不同語言的工作客戶端,則可以使用Fiddler2來比較工作請求和非工作請求,以找出問題所在。

3

您沒有正確設置RESTRequest。很可能,您沒有將請求主體的ContentType設置爲ctAPPLICATION_JSON。默認情況下,除非另有說明,否則單參數RESTRequest將使用ctAPPLICATION_X_WWW_FORM_URLENCODED。這是在文檔中所述:

REST.Client.TRESTRequestParameter.ContentType

When this parameter is left empty, the content type will be chosen basically depending on the number of existing parameters, that go into the requests body. A single-parameter request uses application/x-www-form-urlencoded, while a multiple-parameter request uses multipart/mixed instead.

這可以解釋爲什麼服務器抱怨數據爲形式的編碼。它正在查看身體的Content-Type標題,看到不支持的值,並向您報告錯誤,忽略您發送的實際身體數據。

+0

我已經tryed與僅一個TRESTRequest.params:contentType中設置爲ctAPPLICATION_JSON,一種設置爲pkREQUESTBODY和值設置爲{ 「結束」:{ 「日期」:「2014年6月13日」 }, 「開始」:{ 「日期」: 「2014年6月13日」 }, 「摘要」: 「複檢」 } 和我具有相同的錯誤響應,HTTP/1.0錯誤N°400, 此API不支持解析窗體編碼輸入 我不知道該怎麼辦 –

+0

請顯示您的實際代碼。我使用REST協議失敗,但對'TRESTRequest'還不是很熟悉。這聽起來像你仍然沒有正確設置ContentType。它需要設置在請求的頂層。也許你正在設置它的身體級別,這可能會導致'TRESTRequest'發送'multipart/form-data'作爲請求的頂級ContentType。你能顯示正在傳輸的實際HTTP數據嗎? –

+0

此代碼和參數在DELPHI XE6中運行良好,不在XE5中。我很失望,比價格這個工具! procedure TForm2.Button1Click(Sender:TObject); var rBody:TStringStream; rBody:= TStringStream.Create('{「end」:{「date」:「2014-06-13」},「start」:{「date」:「2014-06-13」},「summary 「:」reTest「}'); RESTRequest.AddBody(rBody,ctAPPLICATION_JSON); RESTRequest.Execute; 結束; –