2

我使用以下参数发送带有请求正文的 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)));
                     });
4

2 回答 2

2

RestSharp中存在一个错误,该错误以前阻止您将任何 Body 内容作为 DELETE 请求的一部分发送。从RestSharp 的 104.2.0 版本开始,此错误已得到修复。

以下是使用 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”标头。

于 2013-11-06T17:23:34.040 回答
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 显示请求有一个主体:

Fiddler 屏幕截图

这似乎可以在普通的 Windows 机器上正常工作。不幸的是,我没有要测试的 Windows Phone。我建议您在普通的 Windows 机器和 Windows Phone 上尝试该代码,看看是否会得到不同的结果。

于 2013-11-06T22:17:26.377 回答