2015-11-07 59 views
0

試圖調用Dropbox API v2。Dropbox API如何在RestRequest中設置內容類型

Dim client = New RestClient("https://api.dropboxapi.com/2/") 
Dim request = New RestRequest("files/search", Method.POST) 
request.AddHeader("Authorization", "Bearer " & MYTOKEN) 
request.AddHeader("Content-Type", "application/json") 
'request.RequestFormat = DataFormat.Json 
'request.JsonSerializer.ContentType = "application/json; charset=utf-8;" 
'request.AddParameter("Content-Type", "application/json") 
request.AddParameter("path", "") 
request.AddParameter("query", "my file") 
request.AddParameter("start", "0") 
request.AddParameter("max_results", "1") 
request.AddParameter("mode", "filename") 
Dim res = client.Execute(request) 

總是返回

Error in call to API function "files/search": Bad HTTP "Content-Type" header: "application/x-www-form-urlencoded". Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack" 

試過註釋掉的代碼行,但還是同樣的反應。任何線索?

+0

從代碼猜測,但是這個Visual Basic,你使用[RestSharp](http://restsharp.org/)? – smarx

回答

1

如果我的猜測是正確的,這是一個使用RestSharp Visual Basic代碼,那麼我認爲你需要像這樣(道歉,如果這是不完全正確的,我不知道VB語法):

Dim client = New RestClient("https://api.dropboxapi.com/2/") 
Dim request = New RestRequest("files/search", Method.POST) 
request.AddHeader("Authorization", "Bearer " & MYTOKEN) 
request.RequestFormat = DataFormat.Json 
request.AddBody(New With { 
    .path = "", 
    .query = "my file", 
    .start = 0, 
    .max_results = 1, 
    .mode = "filename" 
}) 
Dim res = client.Execute(request) 
+0

非常接近,我不得不做一個json對象,然後調用request.AddParameter(「application/json」,json,ParameterType.RequestBody) –

0

像這樣解決了它。可能有更好的方法來做到這一點,但現在這個工作。

Dim client = New RestClient("https://api.dropboxapi.com/2/") 
Dim request = New RestRequest("files/search", Method.POST) 
request.AddHeader("Authorization", "Bearer " & MYTOKEN) 
request.AddHeader("Content-Type", "application/json") '---> this line still doesn't seem to do anything 

Dim json As New JObject(New JProperty("path", ""), New JProperty("query", "my file"), New JProperty("max_results", 1), New JProperty("mode", "filename")) 
request.AddParameter("application/json", json, ParameterType.RequestBody) 
Dim res = client.Execute(request) 

Dropbox API看起來非常敏感,例如,AddParameter中的對象不能是json字符串,它必須是json對象。並且「1」表示在max_results中不起作用,它必須是1而不帶引號。很多嘗試和錯誤,但終於工作。

相關問題