1

I am new to API calls , I have written Create and Update methods which works fine but the DeleteAsync call which returns status 204 but not deleting the item. The request parameter is something like "FieldsOfBusiness/13158?Code=%27555%27". It works fine when replacing %27 with apostrophes in Postman

public async Task<bool> DeleteAsync<T>(object id, NameValueCollection parameters)
    {
        var queryString = ToQueryString(parameters);
        HttpResponseMessage response = await Client.DeleteAsync($"{GetName<T>()}/{id}{queryString}")
            .ConfigureAwait(false);
        response.EnsureSuccessStatusCode();
        var a =  await response.Content.ReadAsAsync<T>();
        return response.IsSuccessStatusCode;
    }     


protected string ToQueryString(NameValueCollection nvc)
    {
        if (nvc == null || nvc.Count < 1)
            return string.Empty;

        var array = (from key in nvc.AllKeys
                     from value in nvc.GetValues(key)
                     select string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value)))
            .ToArray();
        return "?" + string.Join("&", array);
    }  
Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
aj12
  • 288
  • 3
  • 17
  • When it works do you get 200 or 204? – Sean O'Neil Nov 27 '17 at 04:27
  • @SeanO'Neil I get 204 – aj12 Nov 27 '17 at 05:31
  • 1
    See this [https://stackoverflow.com/questions/29545861/what-is-the-http-status-return-code-for-a-successful-delete-statement-in-rest](https://stackoverflow.com/questions/29545861/what-is-the-http-status-return-code-for-a-successful-delete-statement-in-rest) – Sean O'Neil Nov 27 '17 at 05:39
  • 1
    So responding 204 is perfectly normal even if it doesn't delete anything. As to why it's not deleting you'd have to look at what the server is doing. – Sean O'Neil Nov 27 '17 at 05:40

1 Answers1

1

I'm guessing you're not in control of the server or how it responds. As noted in the comments 204 is the typical normal response for a successful DELETE request even if nothing is actually deleted. You want to know if there's anything in the response that tells you if the file was deleted or not. You can check the HttpResponseMessage.ReasonPhrase and see if that's different.

Sean O'Neil
  • 1,222
  • 12
  • 22