2013-05-15 118 views
0

我必須使用RestSharp將一些數據放入API中。Restharp PUT自定義標題

的API資源是:/客戶/服務/財務/ {財政-ID} /子/ {子類別-ID}

從模板參數

除此之外,還有一些查詢參數: 組織-ID(字符串) 運營商ID(字符串)

並且還,請求內容類型必須是application/XML

的辦法,我嘗試使用RestSharp創建這個PUT請求:

RestClient client = new RestClient(url); 
client.Authenticator = Auth1Authenticator.ForRequestToken(Config.getVal("api_key"), Config.getVal("secret_key")); 
IRestRequest request = new RestRequest("", Method.PUT); 
request.RequestFormat = DataFormat.Xml; 
request.AddParameter("organization-id", Config.getVal("params.org")); 
request.AddParameter("operator-id", "Accounting"); 
IRestResponse response = client.Execute(request); 

但我只得到HTTP狀態415 - 不支持的媒體類型

你能幫我解決這個問題嗎? GET請求像魅力一樣工作。

+0

在哪裏你正在發送的內容?我看不到添加到請求正文中的任何內容 - 現在您只需要url path + query字符串。 –

+0

這是要求一個XML,我有架構,但我不知道如何將它與請求一起發送。 – HTB222

回答

0

嘗試發送請求正文是這樣的:

request.XmlSerializer = new RestSharp.Serializers.XmlSerializer(); 
request.RequestFormat = DataFormat.Xml; 
request.AddBody([new instance of the object you want to send]); 

另外,你確定你所訪問的網址是正確的(即有佔位符被寫滿了你PARAMS)?

或者你可以嘗試這樣做:

request.AddParameter("text/xml", [your object to serialize to xml], ParameterType.RequestBody); 

您也可以嘗試,使您的例子作爲類似restsharp維基例子儘可能使其工作:

https://github.com/restsharp/RestSharp/wiki/Recommended-Usage