我正在爲RSS閱讀服務構建客戶端。我正在使用RestSharp庫與其API進行交互。使用RestSharp設置'Content-Type'標題
的API狀態:
當創建或更新您必須設置應用程序/ JSON的記錄; charset = utf-8作爲Content-Type標頭。
這是我的代碼如下所示:
RestRequest request = new RestRequest("/v2/starred_entries.json", Method.POST);
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.RequestFormat = DataFormat.Json;
request.AddParameter("starred_entries", id);
//Pass the request to the RestSharp client
Messagebox.Show(rest.ExecuteAsPost(request, "POST").Content);
然而,該服務正在返回「錯誤415:請使用'Content-Type:application/json; charset = utf-8'標題。」爲什麼RestSharp不通過標題?
我對RestSharp不熟悉,但我會使用Fiddler檢查請求以確定RestSharp IS傳遞的是什麼。可能是Content-Type頭已經被添加了,你需要替換或刪除/添加它。 我會假設'request.RequestFormat = DataFormat.Json'爲你設置Content-Type標題。 –
我試過了,沒有這條線。唯一的選擇是JSON或XML。 –
請發佈請求在Fiddler中的外觀,它會告訴我們是否添加了Content-Type,而不是僅僅猜測。 – Darius