Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get RestSharp to work with DELETE and RequestBody

I send a DELETE request with a request body, with these parameters: enter image description here

The server responds with status 500. I do

curl -D- -X DELETE -d "{\"pin\":\"1111\"}" -H "Content-Type:application/json" -H "Authorization:Basic sameasinimagabove" https://myrestapiendpoint/resource/id

and it works. I use the latest version of RestSharp.WindowsPhone v. 104.3.3.0.
What could be wrong? Code for building and sending the request:

    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)));
                     });
like image 331
Christian Avatar asked Dec 06 '25 08:12

Christian


1 Answers

There was a bug in RestSharp that previously prevented you from being able to send any Body content as part of a DELETE request. This bug has been fixed as of version 104.2.0 of RestSharp.

Here is an example of some code that Un-pins a build using TeamCity's 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);
}

The key part of that code is this line:

request.AddParameter("text/plain", comment, ParameterType.RequestBody);

That line adds the request body and does a few other related things, e.g. sets the "Content-Length" header correctly.

like image 155
Jesse Webb Avatar answered Dec 08 '25 20:12

Jesse Webb