繁体   English   中英

Rest DELETE API无法通过代码工作,但可以与邮递员/招摇工具一起正常工作

[英]Rest DELETE API not working through code but working fine with postman/ swagger

我遇到了一个奇怪的问题,我创建了一个DELETE Rest API,该API正在swagger / Postman中运行,但是在尝试通过代码运行时却没有。 在尝试通过代码时,我得到了正确的状态代码(200)以及响应正文。 这意味着API可以很好地执行,但不知何故它不会删除数据。 删除API的逻辑只是清除字典(这是一个模拟服务)。 以下是我编写的代码,

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); //I don't have any parameters to pass with the url
        httpWebRequest.Method = "DELETE";
        httpWebRequest.ContentType = "application/json";

        using (var response = (HttpWebResponse)httpWebRequest.GetResponse())
        using (var stream = response.GetResponseStream())
        using (var reader = new StreamReader(stream))
        {
            return(reader.ReadToEnd(),response.StatusCode);
        }

一种可能是POSTMAN中使用的url和代码中使用的url有所不同。如果您的delete方法正在执行并且返回200,则表示您的delete方法已成功返回值。但是您要删除的内容存在问题,例如删除记录时使用的id。例如,如果您使用任何存储库来删除实体,则需要检查传递给那里的内容。

您可以在代码中设置断点,以在调用从代码中删除时检查正在使用的ID。

尝试这个:

        var httpWebRequest = WebRequest.Create(url);
        httpWebRequest.Method = "DELETE";
        httpWebRequest.ContentType = "application/json";

        using (var response = (HttpWebResponse)httpWebRequest.GetResponse())
        using (var stream = response.GetResponseStream())
        using (var reader = new StreamReader(stream))
        {
            return (reader.ReadToEnd(), response.StatusCode);
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM