locked
How to call a DELETE REST API with a Body? RRS feed

  • Question

  • User-94355476 posted

    I have a situation where I must call a REST API DELETE endpoint which demands the filename to delete is specified in the Body as follows:

    {
     "FileData":{
        "fileNameWithPath" : "MyFilename.html"
     }
    }

    In Postman this works fine and I get a 200 OK request when the file is deleted.  But in C#, no matter what I've tried, I keep getting a 500 Internal Error.

    Any idea on how to do this properly in C#.NET?

    Robert

    Saturday, February 11, 2017 6:03 AM

Answers

  • User-94355476 posted

    Thank you for responding.  Your message prompted me to carefully revisit my code (now after a good sleep).  I'm happy to report that it's now working.  In case this helps someone else, let me share the code.

    Here is the successful Postman request: https://1drv.ms/i/s!AgOeDHlu5jFPnT6A5Tyr7lgMcLmN

    Here is the now working version of my code:

            public static string DeleteFile(string userName, string repoName, string filename)
            {
                var client = new HttpClient();
                string data = "{ \"FileData\" : { \"fileNameWithPath\" : \"" + filename + "\"}}";
                        
                var requestUri = new Uri(new Uri(Common.WebApiUrl), "api/v1/microtech/" + userName + "/" + repoName + "/file");

                var result =
                    client.SendAsync(
                    new HttpRequestMessage(HttpMethod.Delete, requestUri)
                    {
                        Content = new StringContent(data, Encoding.UTF8, "application/json")
                    })
                    .Result;

                return result.RequestMessage.ToString();
            }

    In point of fact, there were two things wrong with my code:

    1. The concatenation of 'data' was fouled up.
    2. In the "Content = new StringContent ..." statement I was doubly encoding 'data' as JSON.  Not necessary.

    Now everything works!  Thank you again for the inspiration!!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 11, 2017 6:05 PM

All replies

  • User475983607 posted

    In Postman this works fine and I get a 200 OK request when the file is deleted.  But in C#, no matter what I've tried, I keep getting a 500 Internal Error.

    Any idea on how to do this properly in C#.NET?

    I assume you have a client app written in C# that sends a DELETE request to web API but the Web API throws a 500 error.  Essentially the same service endpoint works when a request is sent from Postman but the same endpoint throws a 500 error when sent from custom C# code.

    My knee jerk reaction is the JSON data sent form C# is not formatted the same as the Postman request.  Either debug the C# code or post the source so we can try to reproduce the issue.

    Saturday, February 11, 2017 12:51 PM
  • User-94355476 posted

    Thank you for responding.  Your message prompted me to carefully revisit my code (now after a good sleep).  I'm happy to report that it's now working.  In case this helps someone else, let me share the code.

    Here is the successful Postman request: https://1drv.ms/i/s!AgOeDHlu5jFPnT6A5Tyr7lgMcLmN

    Here is the now working version of my code:

            public static string DeleteFile(string userName, string repoName, string filename)
            {
                var client = new HttpClient();
                string data = "{ \"FileData\" : { \"fileNameWithPath\" : \"" + filename + "\"}}";
                        
                var requestUri = new Uri(new Uri(Common.WebApiUrl), "api/v1/microtech/" + userName + "/" + repoName + "/file");

                var result =
                    client.SendAsync(
                    new HttpRequestMessage(HttpMethod.Delete, requestUri)
                    {
                        Content = new StringContent(data, Encoding.UTF8, "application/json")
                    })
                    .Result;

                return result.RequestMessage.ToString();
            }

    In point of fact, there were two things wrong with my code:

    1. The concatenation of 'data' was fouled up.
    2. In the "Content = new StringContent ..." statement I was doubly encoding 'data' as JSON.  Not necessary.

    Now everything works!  Thank you again for the inspiration!!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 11, 2017 6:05 PM
  • User475983607 posted
    Keep in mind that asp automatically serializes objects to JSON. There is no need or good reason to manually format JSON using concatenation. As a matter of fact, manually formatting JSON leads to errors and extensibility issues. Consider taking advantage of JSON serialization in .net.
    Sunday, February 12, 2017 1:00 PM
  • User-94355476 posted

    I had to use concatenation to introduce the value of filename into the JSON string.

    string data = "{ \"FileData\" : { \"fileNameWithPath\" : \"" + filename + "\"}}";

    Monday, February 13, 2017 12:21 AM