aj12
aj12

Reputation: 289

DeleteAsync return status 204 but not deleting the item

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);
    }  

Upvotes: 1

Views: 1345

Answers (1)

Sean O&#39;Neil
Sean O&#39;Neil

Reputation: 1241

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.

Upvotes: 1

Related Questions