我发送带有请求正文的DELETE请求,其中包含以下参数:
服务器以状态500响应。我做
curl -D- -X DELETE -d "{\"pin\":\"1111\"}" -H "Content-Type:application/json" -H "Authorization:Basic sameasinimagabove" https://myrestapiendpoint/resource/id
它有效。我使用最新版本的RestSharp.WindowsPhone v.104.3.3.0 可能有什么不对? 构建和发送请求的代码:
RestRequest PrepareRequest(Method method, string url, IDictionary<string, object> data)
{
var request = new RestRequest(url, method);
request.AddHeader("Authorization", "Basic " + "yadyadyadyada");
request.AddHeader("X-Originator-Type", "app");
request.AddHeader("X-Os-Type", Environment.OSVersion.Platform.ToString());
request.AddHeader("X-Os-Version", Environment.OSVersion.Version.ToString());
request.AddHeader("X-Device-Id", AppUtil.DeviceId);
request.AddHeader("X-Client-Version", AppUtil.ApplicationVersion);
request.AddHeader("Locale", System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName);
request.AddHeader("If-Modified-Since", DateTime.UtcNow.ToString("u"));
if (data != null || method == Method.PUT || method == Method.POST || method == Method.DELETE)
{
request.RequestFormat = DataFormat.Json;
if (data == null)
data = new Dictionary<string, object>();
string json = JsonParser.Serialize(data);
request.AddParameter("application/json", json, ParameterType.RequestBody);
}
}
_restClient.ExecuteAsync(request, response =>
{
_activeRequests.Remove(handle);
done(responseHandler(AddRequestToResponse(request, response)));
});
答案 0 :(得分:2)
之前有a bug in RestSharp阻止您在DELETE请求中发送任何Body内容。自version 104.2.0 of RestSharp起已修复此错误。
以下是使用TeamCity的REST API解锁构建的一些代码示例:
public void UnpinBuild(int buildId, string comment = null)
{
var request = new RestRequest("builds/id:{id}/pin", Method.DELETE);
request.AddUrlSegment("id", "" + buildId);
if (!String.IsNullOrWhiteSpace(comment))
{
request.AddParameter("text/plain", comment, ParameterType.RequestBody);
}
Console.WriteLine("Executing '{0}' request to '{1}'...", request.Method, _restClient.BuildUri(request));
var response = _restClient.Execute(request);
if (response.StatusCode == HttpStatusCode.NotFound)
{
throw new Exception("Build does not exist for ID: " + buildId);
}
CheckForError(response);
CheckForExpectedStatusCode(response, HttpStatusCode.NoContent);
}
该代码的关键部分是这一行:
request.AddParameter("text/plain", comment, ParameterType.RequestBody);
该行添加了请求正文并执行了一些其他相关操作,例如正确设置“Content-Length”标题。
答案 1 :(得分:0)
您可能在RestSharp中发现了一个错误。
我尝试在我的Windows 7计算机上运行您的代码(略微简化),它似乎有效。
var restClient = new RestClient("http://api.example.com");
var restRequest = new RestRequest("myResource", Method.DELETE);
restRequest.AddHeader("Authorization", "Basic " + "yadyadyadyada");
restRequest.AddHeader("X-Originator-Type", "app");
restRequest.AddHeader("X-Os-Type", Environment.OSVersion.Platform.ToString());
restRequest.AddHeader("X-Os-Version", Environment.OSVersion.Version.ToString());
restRequest.AddHeader("X-Device-Id", "myDeviceID");
restRequest.AddHeader("X-Client-Version", "1.2.3");
restRequest.AddHeader("Locale", System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName);
restRequest.AddHeader("If-Modified-Since", DateTime.UtcNow.ToString("u"));
var data = new Dictionary<string, object> { { "key1", "value1" }, { "key2", "value2" }, };
var json = JsonConvert.SerializeObject(data);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddParameter("application/json", json, ParameterType.RequestBody);
restClient.Execute(restRequest);
运行时,Fiddler显示请求有一个正文:
这似乎在常规Windows机器上正常工作。不幸的是,我没有Windows Phone可以测试。我建议您在常规Windows机器和Windows Phone上尝试代码,看看是否会得到不同的结果。