2016-05-16 62 views
2

因此,我正在使用Dropbox HTTP API與我的Angular2應用程序。我可以登錄,授權和使用list_folder請求等,這一切都按預期工作。
但是,當我試圖執行一個簡單的POST請求files/download我得到一個錯誤說我提供了一個非空content-typeAngular2 HTTP - 提供非空HTTP內容類型頭* Safari *

Error in call to API function \"files/download\": You provided a non-empty HTTP \"Content-Type\" header (\"text/plain;charset=UTF-8\"). 

這是我當前的代碼 - 我已經嘗試了很多不同的方法我沒有在任何地方設置content-type

let headers = new Headers(); 
let url = 'https://content.dropboxapi.com/2/files/download'; 

headers.append('Authorization', `Bearer <inserted-bearer-here>`); 
headers.append("Dropbox-API-Arg", "{\"path\": \"/Documents/code/convert.js\"}"); 

console.log(headers); 

// this is an RxJS observable which is subscribed to later in my code... 
return this.http.post(url, null, { headers: headers }); 

content-type是絕對不能設置,因爲我日誌標題到控制檯發送之前:

Logged headers variable

任何人都能夠闡明這個問題一些輕?我查看了其他關於SO的問題,只發現錯誤地設置了content-type等字段,沒有設置它們的地方,它沒有工作。

- 編輯 -

此代碼工作正常在Chrome:

let headers = new Headers({ 
    'Authorization': `Bearer ${this.userData.bearer}`, 
    'Dropbox-API-Arg': "{\"path\": \"/Documents/code/convert.js\"}, 
    'content-type': null 
}); 
http.post(url, null, { headers: headers }) 
    .subsribe(...); 

然而,在Safari看來,如果Content-Typenull它會自動追加text/plain;charset=UTF-8

我似乎無法找到強制發送空HTTP標頭的方法..如果有人知道這個問題的答案/或Safari的polyfill/fix,將是絕對驚人的!

+2

我使用了你的代碼,並且在網絡標籤中查看實際的網絡請求,Dropbox API的HTTP請求確實有'Content-Type'頭文件集:Content-Type:text/plain; charset = UTF -8'。 Angular http庫顯然是在幕後設置的。 Dropbox API要求不要設置(因爲沒有要描述的主體),所以解決方法是找出停止Angular http庫的操作。不幸的是,我不知道如何反手。 – Greg

+0

@Greg明白,我會仔細看看它 –

回答

1

嘗試添加headers.append('Content-Type', null);

這似乎爲我工作:

http.post(url, null, new RequestOptions({ 
    headers: new Headers({'Content-Type': null}) 
})).subscribe(...); 
+1

仍然一樣'調用API函數的錯誤\「文件/下載\」:您提供了一個非空的HTTP \「Content-Type \」標題(\ 「,text/plain; charset = UTF-8 \」)。「我完全複製了你的代碼,並將其添加到'authorization'頭文件中。出於某種原因,請求發送後它會不斷變化! –

1
headers.append('Content-Type', ' '); 

空不工作,空字符串不工作,而是一個空白確實工作

該駕駛我瘋了,我找不到解決辦法 - 這是一個乾草堆預感中的隨機針,它確實有效。

+1

它對某些瀏覽器有效,但導致MobileSafari出現DOM異常12。我最終創建了一個標準的'XMLHttpRequest'對象,並以舊式的方式進行工作! –

+0

該死!我以爲我打算爲此得分。 :)我還沒有在MobileSafari上測試過,我需要它,因此可能需要切換到您的解決方案。這意味着它必須是有選擇地添加到頭部的角度爲2的http庫,用於某些瀏覽器 - 很奇怪。 – kris

0

添加headers.append('Content-Type','');適合我。